At the time of the interface test, we call multiple interfaces to make multiple requests, and in some cases we need to maintain some common data, such as cookies.
1. The session object of the requests library can help us maintain certain parameters across requests, and will keep cookies between all requests made by the same session instance.
s = requests.session ()
# Req_param = ' {"Belongid": "300001312", "UserName": "alitestss003", "Password": "pxkj88", "Captcha": "Pxpx", "Captchakey" ":" 59675w1v8kdbpxv "} '
# res = s.post (' Http://test.e.fanxiaojian.cn/metis-in-web/auth/login ', Json=json.loads (Req_param))
# # res1 = S.get ("http://test.e.fanxiaojian.cn/eos--web/analysis/briefing")
# Print (Res.cookies.values ()) get all the sessions logged in
2. The session object of the requests library can also provide us with the default data for the request method, by setting the properties of the session object to implement
eg
# Create a Session object
s = requests. Session ()
# Set the Auth property of the session object to use as the default parameter for the request
S.auth = (' user ', ' pass ')
# Set the Headers property of the session, and use the Update method to merge the headers attributes from the remaining request methods as the final request method headers
S.headers.update ({' x-test ': ' true '})
# Send request, not set here Auth will default to the Auth property of the Session object, where the Headers property is merged with the Headers property of the Session object
R = S.get (' http://httpbin.org/headers ', headers={' x-test2 ': ' true '})
The above request data equals: {' Authorization ': ' Basic dxnlcjpwyxnz ', ' x-test ': ' false '}
# View request headers for sending requests
R.request.headers #打印响应中请求的所有header数据
Res3 = S.get ("Http://pre.n.cn/irs-web/sso/login", cookies = cookies)
Print (Res3.request.headers.get ("Cookie"). Split ("irssid=") [-1])
Print (Type (res3.request.headers.get ("Cookie"). Split ("irssid=") [-1]))
Print (res3.request._cookies)
The magical magic of requests.session in Python