The magical magic of requests.session in Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.