python—urllib,urllib2,ssh,get请求,post请求
3、python脚本的源码
root@kali:~/python/dinpay# cat logindinpaypurse.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import ssl
import string
import urllib
import urllib2
def getlogintoken():
urlget = "https://w.dinpay.com/memberLogin/toLogin"
#ctl = {"ctl":"code"}
#ctldata = urllib.urlencode(ctl)
reqget = urllib2.Request(urlget)#构造get请求与参数
#添加get请求的头信息
reqget.add_header("Host","w.dinpay.com")
reqget.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
reqget.add_header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
reqget.add_header("Accept-Language","zh-CN,zh;q=0.8")
reqget.add_header("Accept-Encoding","gzip, deflate, sdch, br")
reqget.add_header("Referer","https://w.dinpay.com/memberCenter/memberIndex")
reqget.add_header('Cookie','rememberTheUserId="[email protected]"; JSESSIONID=42B06AB83851277CE75D335733A5C966')
reqget.add_header("Connection","keep-alive")
reqget.add_header("Cache-Control","max-age=0")
reqget.add_header("Upgrade-Insecure-Requests","1")
#使用本机进行代理抓包,查看详细的数据包
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#
context = ssl._create_unverified_context()#启用ssl
resget = urllib2.urlopen(reqget,context=context)#在urllib2启用ssl字段
resgetdata = resget.read()
#print resgetdata
#return resgetdata
#对get请求的数据回包数据保存
f = open("/root/python/dinpay/getlogintoken.txt","wb")
f.write(resgetdata)
f.close()
def logintoken():
tokenlist = []
html = open("/root/python/dinpay/getlogintoken.txt")
t = 'var loginToken ="'
while 1:
x = html.readline()
if t in x:
#print x
tokenlist.append(x[25:57])
#print tokenlist[0]
return tokenlist[0]
break
def postcommontoken():
url = "https://w.dinpay.com/common/getCommonToken"#请求post的url地址
values = {"":""}
headers = {"Host":"w.dinpay.com",
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"Accept" : "application/json, text/javascript, */*; q=0.01",
"Accept-Language" : "zh-CN,zh;q=0.8",
"Accept-Encoding" : "gzip, deflate, br",
"Referer" : "https://w.dinpay.com/memberLogin/toLogin",
"Cookie" : 'JSESSIONID=D32518287862CAAEC868827FEE81172E; rememberTheUserId="[email protected]"',
"Connection" : "keep-alive",
"X-Requested-With" : "XMLHttpRequest",
"Origin" : "https://w.dinpay.com",
"Content-Length" : "0"}
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data,headers)#请求数据)
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})#抓包
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#启用post请求
context = ssl._create_unverified_context()#启用ssl
response = urllib2.urlopen(req,context=context)#在urllib2启用ssl字段,打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
#print the_page#打印请求到的页面
f = open("/root/python/dinpay/postcommontoken.txt","wb")
f.write(the_page)
f.close()
def commontoken():
tokenlist = []
html = open("/root/python/dinpay/postcommontoken.txt")
t = '"data":"'
while 1:
x = html.readline()
if t in x:
#print x
tokenlist.append(x[9:41])
#print tokenlist[0]
return tokenlist[0]
break
def getloginvalidate(logintoken,commontoken):#请求登陆业务
url = "https://w.dinpay.com/memberLogin/getLoginValidateType"#请求post的url地址
values = {"loginToken":logintoken,"memberId":"gaxwb60%40163.com","commonToken":commontoken}#请求的URL地址,post表单数据息
headers = {"Host":"w.dinpay.com",
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"Accept" : "application/json, text/javascript, */*; q=0.01",
"Accept-Language" : "zh-CN,zh;q=0.8",
"Accept-Encoding" : "gzip, deflate, br",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Referer" : "https://w.dinpay.com/memberLogin/toLogin",
"Cookie" : 'JSESSIONID=60025478F77BFB01639A567EC1BCF982; sto-id-47873=AGKCBAKMFAAA; rememberTheUserId="[email protected]"; JSESSIONID=60025478F77BFB01639A567EC1BCF982',
"Connection" : "keep-alive",
"X-Requested-With" : "XMLHttpRequest",
"Origin" : "https://w.dinpay.com",
"Content-Length" : "115"}
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data,headers)#请求数据)
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})#抓包
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#启用post请求
context = ssl._create_unverified_context()#启用ssl
response = urllib2.urlopen(req,context=context)#在urllib2启用ssl字段,打开请求的数据
#response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
#print the_page#打印请求到的页面
return the_page
def login(logintoken,commontoken):#请求登陆业务
url = "https://w.dinpay.com/memberLogin/login"#请求post的url地址
values = {"loginToken":logintoken,"memberId":"gaxwb60%40163.com","loginPassword":"qwe123456","commonToken":commontoken}#请求的URL地址,post表单数据信息
headers = {"Host":"w.dinpay.com",
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"Accept" : "application/json, text/javascript, */*; q=0.01",
"Accept-Language" : "zh-CN,zh;q=0.8",
"Accept-Encoding" : "gzip, deflate, br",
"Referer" : "https://w.dinpay.com/memberCenter/memberIndex",
"Cookie" : 'JSESSIONID=D32518287862CAAEC868827FEE81172E; rememberTheUserId="[email protected]"',
"Connection" : "keep-alive",
"X-Requested-With" : "XMLHttpRequest",
"Origin" : "https://w.dinpay.com",
"Content-Length" : "0"}
data = urllib.urlencode(values)#请求post表单数据
req = urllib2.Request(url,data,headers)#请求数据)
proxy_handler = urllib2.ProxyHandler({'http': '192.168.40.36:4455'})#抓包
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)#启用post请求
context = ssl._create_unverified_context()#启用ssl
response = urllib2.urlopen(req,context=context)#在urllib2启用ssl字段,打开请求的数据
#response = urllib2.urlopen(req)#打开请求的数据
the_page = response.read()#读取并缓存请求到的数据
print the_page#打印请求到的页面
print "你请求到页面数据包为%d字节" %len(the_page)#计算请求到的页面数据大小
#if __name__ == "__mian__":
getlogintoken()
logintoken = logintoken()
print logintoken
postcommontoken()
commontoken = commontoken()
print commontoken
#getloginvalidate(logintoken,commontoken)
#postcommontoken()
#commontoken = commontoken()
#print commontoken
login(logintoken,commontoken)
root@kali:~/python/dinpay#