Add cookies to the request
For some websites, log in and get cookies from the browser, and you can log in directly with the cookie in the future without entering the username and password.
1. Add cookie to parameters
Use cookies when sending requests
Code example:
import requests # 1, add cookie_dict = {"login_name": "admin"} to the parameter # cookie as a variable, and then use cookies in the request response = requests.get(url="http://www.hnxmxit.com ",cookies=cookie_dict)
View execution results:
2. Add cookies to the header
Use headers when sending requests
Code example:
# 2, add cookie_header = {"login_name":"root"} to the head res = requests.get(url="http://www.hnxmxit.com", headers=cookie_header)
View execution results
Add cookie through session
Code example:
# session添加cookie import requests session_obj = requests.session() # 方法1: # session_obj.cookies["test_add_cookie"]="newdream2021" # response = session_obj.get(url="http://www.hnxmxit.com") # 方法2: 推荐使用 session_obj.cookies.set("test_add_cookie","newdream") response = session_obj.get(url="http://www.hnxmxit.com") # 方法3: # cookie_dict = {"test_add_cookie":"newdream2020"} # requests.utils.add_dict_to_cookiejar(session_obj.cookies,cookie_dict) # response = session_obj.get(url="http://www.hnxmxit.com") # 方法4: # cookie_obj = requests.cookies.RequestsCookieJar() # cookie_obj.set("test_add_cookie","newdream2022") # session_obj.cookies.update(cookie_obj) # response = session_obj.get(url="http://www.hnxmxit.com")
Add cookie to bypass login
Code example:
# Add cookie to bypass login to phpwind forum import requests,re # 1. Manually open the forum to get the cookie after login # 2. Add cookie session_obj = requests.session() session_obj.cookies.set(name="zFb_lastvisit",value=" eU3LmIB4gCR2k5lCRVBNs9E5smt4igrFH7fXe82e4aXo%2FGbpZ0gclab4jb8%3D",domain="47.107.178.45",path="/") session_obj.cookies.set(name="zFb_winduser",value="481%091648725 688%09%2Fphpwind%2Findex.php%3Fm% 3Du%26a%3Dshowcredit",domain="47.107.178.45",path="/") # Visit the home page response = session_obj.get(url="http://47.107.178.45/phpwind/") print(response.content .decode("utf-8"))
Practical case
Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.
If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers
At the right age, choose the right position, and try to give full play to your own advantages.
My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,
Test and develop video tutorials, study notes and receive portals! ! !