掌握了前面幾節的的內容,就能夠作一些簡單的http協議接口的請求發送了,可是這些還不夠。HTTP協議是一個無狀態的應用層協議,也就是說先後兩次請求是沒有任何關係的,那若是咱們測試的接口以前有相互依賴關係怎麼辦呢(好比我要在博客園發文章,是須要先登陸的),這時咱們就要用到cookie和session技術來保持客戶端與服務器端鏈接的狀態,這也就是本節要介紹的內容:python
1、Cookie:json
一、獲取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
輸出:cookie
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]> {'BDORZ': '27315'} BDORZ 27315
2、發送cookie:session
# -*- 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)
2、Sessiondom
一、保持會話同步:測試
# -*- 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
輸出:ui
{ "cookies": {} } ------ { "cookies": { "sessioncookie": "123456789" } }
二、保存會話信息:url
# -*- 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
輸出:spa
{ "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "Testa": "AAA", "Testb": "BBB", "User-Agent": "python-requests/2.18.1" } }
三、刪除已存在的會話信息,保存爲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" } }
四、提供默認數據:
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