掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够。HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关系怎么办呢(比如我要在博客园发文章,是需要先登录的),这时我们就要用到cookie和session技术来保持客户端与服务器端连接的状态,这也就是本节要介绍的内容:
一、Cookie:
1、获取cookie:
# -*- coding:utf-8 -*- #获取cookie import requests import json url = "https://www.baidu.com/" r = requests.get(url) #将RequestsCookieJar转换成字典 c = requests.utils.dict_from_cookiejar(r.cookies) print r.cookies print c for a in r.cookies: print a.name,a.value
输出:
]> {'BDORZ': '27315'} BDORZ 27315
二、发送cookie:
# -*- coding:utf-8 -*- #发送cookie到服务器 import requests import json host = "http://httpbin.org/" endpoint = "cookies"
url = ''.join([host,endpoint]) #方法一:简单发送 # cookies = {"aaa":"bbb"} # r = requests.get(url,cookies=cookies) # print r.text #方法二:复杂发送 s = requests.session() c = requests.cookies.RequestsCookieJar() c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com') s.cookies.update(c)
二、Session
1、保持会话同步:
# -*- coding:utf-8 -*- import requests import json host = "http://httpbin.org/" endpoint = "cookies" url = ''.join([host,endpoint]) url1 = "http://httpbin.org/cookies/set/sessioncookie/123456789" r = requests.get(url) print r.text
print "------"
s = requests.session() #初始化一个session对象 s.get(url1) #cookie的信息存在了session中 r = s.get(url) print r.text
输出:
{ "cookies": {} } ------ { "cookies": { "sessioncookie": "123456789" } }
2、保存会话信息:
# -*- coding:utf-8 -*- import requests import json host = "http://httpbin.org/" endpoint = "headers" url = ''.join([host,endpoint]) header1 = {"testA":"AAA"} header2 = {"testB":"BBB"} s = requests.session() #初始化一个session对象 s.headers.update(header1) #已经存在于服务中的信息 r = s.get(url,headers=header2) #发送新的信息 print r.text
输出:
{ "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "Testa": "AAA", "Testb": "BBB", "User-Agent": "python-requests/2.18.1" } }
3、删除已存在的会话信息,保存为None
# -*- coding:utf-8 -*- import requests import json host = "http://httpbin.org/" endpoint = "headers" url = ''.join([host,endpoint]) header1 = {"testA":"AAA"} header2 = {"testB":"BBB"} s = requests.session() #初始化一个session对象 s.headers.update(header1) #已经存在于服务中的信息 r = s.get(url,headers=header2) #发送新的信息 print r.text print '--------' s.headers['testA'] = None #删除会话里的信息testA r1 = s.get(url,headers = header2) print r1.text
{ "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "Testa": "AAA", "Testb": "BBB", "User-Agent": "python-requests/2.18.1" } } -------- { "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "Testb": "BBB", "User-Agent": "python-requests/2.18.1" } }
4、提供默认数据:
s = requests.Session() s.auth = ('user', 'pass') s.headers.update({'x-test': 'true'}) # both 'x-test' and 'x-test2' are sent s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
参考:
http://docs.python-requests.org/en/master/user/quickstart/#cookies
http://docs.python-requests.org/en/master/user/advanced/#session-objects