Suggestions for using the Python Requests module session

This article mainly explains the usage suggestions of the Python Requests module session and the methods of all cookies in the entire session.

test code

Server: The following is a server made with flask , which is used to set cookies and print request headers.

# -*- coding: utf-8 -*-
from flask import Flask, make_response, request


app = Flask(__name__)



@app.route('/a1')
def a1():
    print(request.headers)
    rp = make_response()
    rp.set_cookie('a1', '123')
    return rp



@app.route('/a2')
def a2():
    print(request.headers)
    rp = make_response()
    # rp.set_cookie('a2', '234')
    return rp



@app.route('/a3')
def a3():
    print(request.headers)
    rp = make_response()
    rp.set_cookie('a3', '345')
    return rp



if __name__ == '__main__':
    app.run(host='0.0.0.0')

(Swipe left and right to view the complete code)

client:

# -*- coding: utf-8 -*-


import requests


url1 = 'http://192.168.2.159:5000/a1'
url2 = 'http://192.168.2.159:5000/a2'
url3 = 'http://192.168.2.159:5000/a3'


cookies = requests.utils.cookiejar_from_dict({'test': 'test'})
print(type(cookies), cookies)  # RequestsCookieJar 对象
s = requests.session()
s.cookies = cookies    # 这里设置的cookie test=test 是所有请求中都会附带的
s.headers = {'h1':'h1'} #  这里设置的请求头h1=h1是所有请求中都会附带的
r1 = s.get(url1, cookies={'r1': 'r1'},headers={'h2':'h2'})  # 临时加上cookie r1=r1 和 header h2=h2 下一个请求中不会有此 cookie  和header
r2 = s.get(url2)
requests.utils.add_dict_to_cookiejar(s.cookies, {'xx': 'xx'})  # 在接下来的请求中,永久添加xx cookie


r3 = s.get(url3)


# r1.cookies 是一个RequestsCookieJar对象,可以使用  requests.utils.dict_from_cookiejar(r1.cookies) 将其转换成dict
# 我发现可以直接用dict进行转换,这样写起来更方便  
print(dict(r1.cookies))  # 打印r1请求的返回结果中设置的cookies
print(dict(r2.cookies))  # 打印r2请求的返回结果中设置的cookies
print(dict(r3.cookies))  # 打印r3请求的返回结果中设置的cookies


print(dict(s.cookies))  # s.cookies中包含整个会话请求中的所有cookie(临时添加的如上面的r1不包含在内)

(Swipe left and right to view the complete code)

Start the server first, then the client.

operation result

The server prints the result:

192.168.2.159 - - [26/Jun/2019 17:28:00] "GET /a1 HTTP/1.1" 200 -
Host: 192.168.2.159:5000
Accept-Encoding: identity
H1: h1
H2: h2
Cookie: test=test; r1=r1



192.168.2.159 - - [26/Jun/2019 17:28:00] "GET /a2 HTTP/1.1" 200 -
Host: 192.168.2.159:5000
Accept-Encoding: identity
H1: h1
Cookie: test=test; a1=123



192.168.2.159 - - [26/Jun/2019 17:28:00] "GET /a3 HTTP/1.1" 200 -
Host: 192.168.2.159:5000
Accept-Encoding: identity
H1: h1
Cookie: test=test; xx=xx; a1=123

(Swipe left and right to view the complete code)

The client prints the result:

<class 'requests.cookies.RequestsCookieJar'> <RequestsCookieJar[<Cookie test=test for />]>
{'a1': '123'}
{}
{'a3': '345'}
{'test': 'test', 'xx': 'xx', 'a1': '123', 'a3': '345'}

(Swipe left and right to view the complete code)

Summary and Suggestions

It can be seen from the server-side printing that if we do not set the User-Agent, the request header of the requests module is python-requests/2.21.0, which is not the request header of a normal browser, which is why we must modify the request when we make a crawler head for a reason.

Using requests.session() can help us save all cookies during this session, saving us from getting the cookie of the previous request, updating the cookie, resetting it, and then requesting such operations.

Cookies and headers that will be carried throughout the session set by s.cookies and s.headers.

Cookies and headers set in the form of s.get(url1, cookies={'r1': 'r1'},headers={'h2':'h2'}) will not overwrite s.cookies and s.headers The request header and cookie set are only added to this request, and the r1 and h2 here will not be carried in the next request.

requests.utils.add_dict_to_cookiejar(s.cookies, {'xx': 'xx'}) can set a fixed cookie for s: xx, the cookie of this setting is not temporary, and will be carried in subsequent requests.

The result of r1.cookies is a RequestsCookieJar object, which can be converted by dict to get a dict whose content is the cookie set in the r1 request response header. If no new cookie is set for the current request, the dict is followed by an empty dictionary.

The result of s.cookies is the cookie set during the entire session (all requests sent through s), and all the set cookies can be obtained through dict(s.cookies)

It is recommended that we set up the common parts in advance, such as headers, cookies, and proxies, during use.

Recently, I found that if some cookies are set multiple times during the whole process, direct use of dict to force the transfer will fail. The safest way is to use requests.utils.dict_from_cookiejar(s.cookies) to get dictionary-type cookies.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

insert image description here

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!   

Guess you like

Origin blog.csdn.net/qq_48811377/article/details/132512113