首先说一下使用python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路:
1、对于带token的页面,需要先从最开始的页面获取合法token
2、然后使用获取到的合法token进行后续操作
3、token一般存储的地方有两个:
【一种是携带在cookie中】
【一种是在respose(页面返回)的隐藏表单中】,获取思路类似
释:header是针对服务端有各种限制或特定需求时使用的,一般服务器会进行类似如:X-Requested-With、Content-Length、User-Agent等的验证,所以需要将其以字典的形势发送给服务器
#coding:utf-8
'''
Created on @author: Administrator
'''
import json
import requests #拼接url
host = "http://192.168.1.112:3100/"
#初始化url请求对象
r = requests.get(host)
#获取url请求对象中的有用信息,如token、cookies
token = r.cookies.items()[0][1]
cookies = r.cookies
#以下为测试,所获取的token及cookie的格式
print(type(token)) #获取token的类型
print(token) #打印token
print(cookies) #打印cookies
print(r.headers) #打印头文件
print(r.url) #打印URL
控制台输出结果:
<type 'str'>
h2r_bATY0wEkAAAAMzkyYmUwYWItZGRmNC00ODk4LTlkNWMtYzY5ZDczNGRkMTg1EKBHlvGpVQ_F9e1Kc4k94ZQ_qMERlCAGT4qt6KKMKP81
<RequestsCookieJar[<Cookie .ASPXANONYMOUS=h2r_bATY0wEkAAAAMzkyYmUwYWItZGRmNC00ODk4LTlkNWMtYzY5ZDczNGRkMTg1EKBHlvGpVQ_F9e1Kc4k94ZQ_qMERlCAGT4qt6KKMKP81 for 192.168.1.112/>, <Cookie __RequestVerificationToken=ahgQd20TJe1CIuaaL_uKxGP0IXv1TV4u7mqJHNfMavwucOuPpPOT8afHWcCWSstyuO41ihaynmGwFtjgj8H5upPi1LmEcHrEj5moWCkCCfPIUaHpjCJt2P-Ng62LjNfdfDHY0FiDcAf8fk8eamboNw2 for 192.168.1.112/>]>
{'Content-Length': '', 'Set-Cookie': '.ASPXANONYMOUS=h2r_bATY0wEkAAAAMzkyYmUwYWItZGRmNC00ODk4LTlkNWMtYzY5ZDczNGRkMTg1EKBHlvGpVQ_F9e1Kc4k94ZQ_qMERlCAGT4qt6KKMKP81; expires=Thu, 19-Apr-2018 17:32:44 GMT; path=/; HttpOnly, .ASPXAUTH=; expires=Mon, 11-Oct-1999 16:00:00 GMT; path=/; HttpOnly, __RequestVerificationToken=ahgQd20TJe1CIuaaL_uKxGP0IXv1TV4u7mqJHNfMavwucOuPpPOT8afHWcCWSstyuO41ihaynmGwFtjgj8H5upPi1LmEcHrEj5moWCkCCfPIUaHpjCJt2P-Ng62LjNfdfDHY0FiDcAf8fk8eamboNw2; path=/; HttpOnly', 'Server': 'Microsoft-IIS/7.5', 'Cache-Control': 'private', 'Date': 'Fri, 09 Feb 2018 06:52:44 GMT', 'Content-Type': 'text/html; charset=utf-8'}
http://192.168.1.112:3100/