Python入门:模拟登录(二)或注册之requests处理带token请求

转自http://blog.csdn.net/foryouslgme/article/details/51822209

首先说一下使用Python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路: 
1、对于带token的页面,需要先从最开始的页面获取合法token 
2、然后使用获取到的合法token进行后续操作 
3、token一般存储的地方有两个: 
【一种是携带在cookie中】、【一种是在respose(页面返回)的隐藏表单中】,获取思路类似 
释:header是针对服务端有各种限制或特定需求时使用的,一般服务器会进行类似如:X-Requested-With、Content-Length、User-Agent等的验证,所以需要将其以字典的形势发送给服务器

 1 #reg.py
 2 '''
 3 此代码只实现了注册的第一步【手机发送验证码】,主要解决的获取token
 4 具体操作有:获取页面第一次请求时的重要信息,如:cookie与token
 5 '''
 6 import requests
 7 #拼接url
 8 host = "http://10.70.18.33:8083/"
 9 url1 = host + "shopxx-mobile/register.jhtml"
10 #初始化url请求对象
11 r = requests.get(url1)
12 #获取url请求对象中的有用信息,如token、cookies
13 token = r.cookies.items()[0][1]
14 cookies = r.cookies
15 #以下为测试,所获取的token及cookie的格式
16 print(type(token))
17 print(token)
18 print(cookies)
19 print(r.headers)
20 print(r.url)
21 #手机号码发送验证码的url拼接
22 url2 = host + "shopxx-mobile/register/send.jhtml"
23 #拼接header中的重要数据,如:token、cookie、User-Agent、Content-Length、X-Requested-With等,其中除token及cookie是通过上面的代码获取到的之外,其它的均可以通过firefox浏览器进行获取
24 headers = {
25         "token": token,
26         "Host": "10.70.18.33:8083",
27         "User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
28         "Accept": "application/json, text/javascript, */*; q=0.01",
29         "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
30         "Accept-Encoding": "gzip, deflate",
31         "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
32         "X-Requested-With": "XMLHttpRequest",
33         "Connection":"keep-alive",
34         "Content-Length": "18",
35         "charset":"UTF-8",
36         "cookie":"token=" + token
37         }
38 #一般登录注册页面均是post方式进行提交的,需要将post需要提交的数据(此处为需要发送验证码的手机号码)进行组装
39 data = {'mobile':'1851174****'}
40 #初始化post请求对象(需要传入url、提交的数据、header)
41 s = requests.post(url2,data,headers=headers)
42 #打印返回结果
43 print(s)
44 print(s.status_code,s.text)

执行结果

/Users/frankslg/PycharmProjects/cjb/mobile/regist.py
<class 'str'>
7c74a4cce353aec3133005feb40a9f39
<RequestsCookieJar[<Cookie token=7c74a4cce353aec3133005feb40a9f39 for 10.70.18.33/>]>
{'Date': 'Mon, 04 Jul 2016 08:53:30 GMT', 'Set-Cookie': 'token=7c74a4cce353aec3133005feb40a9f39; Path=/', 'Server': 'Apache-Coyote/1.1', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Language': 'zh-CN', 'Transfer-Encoding': 'chunked'}
http://10.70.18.33:8083/shopxx-mobile/register.jhtml
<Response [200]>
200 {"type":"success","content":"恭喜您,账号注册成功!"}

函数封装

 

import requests


def reg():
    host = "http://10.70.18.33:8083/"
    url1 = host + "shopxx-mobile/register.jhtml"
    r = requests.get(url1)
    token = r.cookies.items()[0][1]

    url2 = host + "shopxx-mobile/register/send.jhtml"
    headers = {
            "token": token,
            "Host": "10.70.18.33:8083",
            "User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "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",
            "Connection":"keep-alive",
            "Content-Length": "18",
            "charset":"UTF-8",
            "cookie":"token=" + token
            }

    data = {'mobile':'1851174****'}
    s = requests.post(url2,data,headers=headers)

    print(s.status_code,s.text)

if __name__ == '__main__':
    reg()

 

posted @ 2017-07-05 14:27  天空是我喜欢的颜色  阅读(11784)  评论(0编辑  收藏  举报