python—实战项目-增加手机用户并获取一次性有效的token值
1、环境部署
功能是:给该系统增加手机的用户。该功能增加用户时会携带token值进行一次性判断,token一次性有效,获取最新token值都是请求以下的数据包中使用正则匹配出来,token值每次从后台发送至前端页面。(此业务没有使token值与增加手机号码账户的分开请求处理。如果分开两步,一步获取token值,一步增加手机号码用户,则一定要分两次请求提交数据)
请求数据库包:
POST /nextCheckMobileExist HTTP/1.1
Host: www.amsweb.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.amsweb.com/memberManage
Content-Length: 90
Cookie: I18N_LANG=zh_CN; JSESSIONID=1B93B4CE8056DFEAB894BA40877F920E; evercookie_etag=258#95101275; evercookie_cache=258#95101275; evercookie_png=258#95101275; scenerisk_ams=258#95101275
Connection: close
mobileListRealString=15817222558%3B&checkMobileExistToken=273DD6AA4950401289BA6C0F4F163F7C
数据回包:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: I18N_LANG=zh_CN; Expires=Sat, 20-Apr-2019 06:44:22 GMT; Path=/
Content-Type: text/html;charset=UTF-8
Date: Fri, 20 Apr 2018 06:44:22 GMT
Connection: close
Content-Length: 63
{"result":"success","token":"A2141A2C9A9446418D74BFBC31C1ADD9"}
2、python脚本
#-*- codint:utf-8 -*-
import urllib,urllib2,time,re
url = 'http://www.amsweb.com/nextCheckMobileExist'
headers = {#构造URL请求
'Host':'www.amsweb.com',
'Proxy-Connection':'keep-alive',
'Content-Length':'90',
'Accept':'*/*',
'Origin':'http://www.amsweb.com',
'X-Requested-With':'XMLHttpRequest',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Referer':'http://www.amsweb.com/memberManage',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cookie':'I18N_LANG=zh_CN; JSESSIONID=1B93B4CE8056DFEAB894BA40877F920E; evercookie_etag=258#95101275; evercookie_cache=258#95101275; evercookie_png=258#95101275; scenerisk_ams=258#95101275',#此处的cookie信息必须换成有效的
}
token = 'A2141A2C9A9446418D74BFBC31C1ADD9'#此处token值也必须换成有效的
proxy_support = urllib2.ProxyHandler({'http':'127.0.0.1:4455'})#设置本机的代理,进行抓包分析
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
f = open('./mobile.txt','r')#在脚本的目录下读取手机号码(一行就一个手机号码)文件,必须新建此文件
mobiles = f.readlines()
f.close()
for mobile in mobiles:#循环遍历手机号码
mobile = mobile.strip("\n")
data = {#构造POST内容
'mobileListRealString':mobile+';',
'checkMobileExistToken':token,
}
req = urllib2.Request(url=url,headers=headers)
post = urllib.urlencode(data)
response = urllib2.urlopen(req,post)#发送请求
rep = response.read()#读取、打印响应内容
print rep.decode('utf-8')
pattern = re.compile('token\":\"(.*)\"')#正则匹配响应内容中的token值
token = "".join(pattern.findall(rep))
print data
time.sleep(1)
3、运行情况
C:\Python27>python gettoken.py
{"result":"18576424220;手机号已存在,请重新输入","token":"6A9B24A06E294C6FB09F2DBBCB41BAAC"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': 'A2141A2C9A9446418D74BFBC31C1ADD9'}
{"result":"15817260872;手机号已存在,请重新输入","token":"FADA4A1A0C1E4E53BB7592599CA7D296"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': '6A9B24A06E294C6FB09F2DBBCB41BAAC'}
{"result":"success","token":"ADF772FEDAE84DBC927AFB5FFA427BB0"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': 'FADA4A1A0C1E4E53BB7592599CA7D296'}
{"result":"13691965340;手机号已存在,请重新输入","token":"DCED3FCA2B444B59AC15937703A355C5"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': 'ADF772FEDAE84DBC927AFB5FFA427BB0'}
{"result":"15112500169;手机号已存在,请重新输入","token":"F84B075DBB6B4491923CCE54693B1C51"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': 'DCED3FCA2B444B59AC15937703A355C5'}
{"result":"18576424220;手机号已存在,请重新输入","token":"1F95F607C9B545B69660A2E2AFAA26D0"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': 'F84B075DBB6B4491923CCE54693B1C51'}
{"result":"15817260872;手机号已存在,请重新输入","token":"F0F78D33554E4E5AA8B06056476AD7A7"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': '1F95F607C9B545B69660A2E2AFAA26D0'}
{"result":"success","token":"4B1E34F66284449DB5E65C8BEC2E7A28"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': 'F0F78D33554E4E5AA8B06056476AD7A7'}
{"result":"13691965340;手机号已存在,请重新输入","token":"878071C2982E45C1A4D1F000E3F12202"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': '4B1E34F66284449DB5E65C8BEC2E7A28'}
{"result":"15112500169;手机号已存在,请重新输入","token":"8B4F8940F6CE44AFA23F3DAB81C78D41"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': '878071C2982E45C1A4D1F000E3F12202'}
{"result":"18576424220;手机号已存在,请重新输入","token":"285D4637E9F746FBA4CDF3F3D5FC3DB5"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': '8B4F8940F6CE44AFA23F3DAB81C78D41'}
{"result":"15817260872;手机号已存在,请重新输入","token":"53AD25986F0B46CA853F4F4EAA0D4769"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': '285D4637E9F746FBA4CDF3F3D5FC3DB5'}
{"result":"success","token":"F166D2EBEA664626A410476E4F6AB5CC"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': '53AD25986F0B46CA853F4F4EAA0D4769'}
{"result":"13691965340;手机号已存在,请重新输入","token":"4A01E207FFCC48C6ABE298664C218DFF"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': 'F166D2EBEA664626A410476E4F6AB5CC'}
{"result":"15112500169;手机号已存在,请重新输入","token":"54ABAF80B6634074806C813C1994C466"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': '4A01E207FFCC48C6ABE298664C218DFF'}
{"result":"18576424220;手机号已存在,请重新输入","token":"8E8D2B9063F9425287D1ABE53DC2B40D"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': '54ABAF80B6634074806C813C1994C466'}
{"result":"15817260872;手机号已存在,请重新输入","token":"AD46B7E4868C465ABF0A0D4C5125D79E"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': '8E8D2B9063F9425287D1ABE53DC2B40D'}
{"result":"success","token":"B4DAA1581A2F48949416B65018F68DEE"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': 'AD46B7E4868C465ABF0A0D4C5125D79E'}
{"result":"13691965340;手机号已存在,请重新输入","token":"6733D56D12474BEEABE3B0CF59A2D2A5"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': 'B4DAA1581A2F48949416B65018F68DEE'}
{"result":"15112500169;手机号已存在,请重新输入","token":"33C4B906320449ECA2805037E9EBDEB2"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': '6733D56D12474BEEABE3B0CF59A2D2A5'}
{"result":"18576424220;手机号已存在,请重新输入","token":"56B83477397D4AE7B22A8668104A5CAD"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': '33C4B906320449ECA2805037E9EBDEB2'}
{"result":"15817260872;手机号已存在,请重新输入","token":"5826DED0490648648A1E20098D16C535"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': '56B83477397D4AE7B22A8668104A5CAD'}
{"result":"success","token":"41FA991C922743F3B93E90A4F6FCEACB"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': '5826DED0490648648A1E20098D16C535'}
{"result":"13691965340;手机号已存在,请重新输入","token":"5AB897B5AD574F9798F57A486395D282"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': '41FA991C922743F3B93E90A4F6FCEACB'}
{"result":"15112500169;手机号已存在,请重新输入","token":"349AA396D23244039A201BAF2891C22B"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': '5AB897B5AD574F9798F57A486395D282'}
{"result":"18576424220;手机号已存在,请重新输入","token":"E1AB4D5C54444A49B6E17DE236CE19F4"}
{'mobileListRealString': '18576424220;', 'checkMobileExistToken': '349AA396D23244039A201BAF2891C22B'}
{"result":"15817260872;手机号已存在,请重新输入","token":"EB6F2712814944FEAB6350CAD22BB322"}
{'mobileListRealString': '15817260872;', 'checkMobileExistToken': 'E1AB4D5C54444A49B6E17DE236CE19F4'}
{"result":"success","token":"E0A29FCCA9CA427386230ED1D7043D8E"}
{'mobileListRealString': '18503037591;', 'checkMobileExistToken': 'EB6F2712814944FEAB6350CAD22BB322'}
{"result":"13691965340;手机号已存在,请重新输入","token":"F8C49D262B574146BB431D7B4C3A9076"}
{'mobileListRealString': '13691965340;', 'checkMobileExistToken': 'E0A29FCCA9CA427386230ED1D7043D8E'}
{"result":"15112500169;手机号已存在,请重新输入","token":"563FC3DDB5A64B968223D061DB082B3E"}
{'mobileListRealString': '15112500169;', 'checkMobileExistToken': 'F8C49D262B574146BB431D7B4C3A9076'}
{"result":"13750380272;手机号已存在,请重新输入","token":"389C3E21053A4F1FB8544A33AC2B9391"}
{'mobileListRealString': '13750380272;', 'checkMobileExistToken': '563FC3DDB5A64B968223D061DB082B3E'}
C:\Python27>