Three methods requests handles cookies with Python

 

In interface testing, the interface most projects are carried out after the operation need to log in, requests are often used to simulate the login and library operations after login, the following is summed up after I ever stepped on a pit three operations on login credentials of cookies method.

 

A. With requests.utils.dict_from_cookiejar () converts the returned cookies into a dictionary

  1. handling cookies:

 1 import requests
 2 
 3 def login():
 4     login_url = 'http://www.xxx.com/login
 5     headers = {
 6         "Accept": "application/json, text/javascript, */*; q=0.01"
 7     }
 8     body = {
 9         "usercode": "liuzz05@****.com",
10         "password": "123456"
11     }
12     try:
13         res = requests.post(url=login_url, headers=headers, data=body)
14         cookies = res.cookies
15 
16         cookie = requests.utils.dict_from_cookiejar(cookies)
17 
18         return cookie
19     except Exception as err:
20         print('获取cookie失败:\n{0}'.format(err))

    2. Use cookie:

1 import requests
2 
3 def get_data():
4     cookie = login()
5     res = requests.get(url=get_data_url, cookies=cookie)
6     print(res.text)

 

 

 II. Traversing keys cookies, spliced ​​into cookie format

  1. handling cookies:

 1 import requests
 2 
 3 def login():
 4     login_url = 'http://www.xxx.com/login
 5     headers = {
 6         "Accept": "application/json, text/javascript, */*; q=0.01"
 7     }
 8     body = {
 9         "usercode": "liuzz05@****.com",
10         "password": "123456"
11     }
12     try:
13         res = requests.post(url=login_url, headers=headers, data=body)
14         cookies = res.cookies.items()
15 
16         cookie = ''
17         for name, value in cookies:
18             cookie += '{0}={1};'.format(name, value)
19 
20         return cookie
21     except Exception as err:
22         print('获取cookie失败:\n{0}'.format(err))

    2. Use cookie:

1 import requests
2 
3 def get_data():
4     cookie = login()
5     headers = {
6         "cookie": cookie
7     }
8     res = requests.get(url=get_data_url, headers=headers)
9     print(res.text)

  


III. Direct splicing cookies, this method is more stupid, the premise is to know the key cookies

  1. handling cookies:

 1 import requests
 2 
 3 def login():
 4     login_url = 'http://www.xxx.com/login
 5     headers = {
 6         "Accept": "application/json, text/javascript, */*; q=0.01"
 7     }
 8     body = {
 9         "usercode": "liuzz05@****.com",
10         "password": "123456"
11     }
12     try:
13         res = requests.post(url=login_url, headers=headers, data=body)
14         cookies = res.cookies
15 
16         phpsessid = cookies['phpsessid']
17         env_orgcode = cookies['env_orgcode']
18         acw_tc = cookies['acw_tc']
19         aliyungf_tc = cookies['aliyungf_tc']
20         last_env = cookies['last_env']
21 
22         cookie = 'phpsessid={0};env_orgcode={1};acw_tc{2};aliyungf_tc={3};last_env={4}'.format(
23             phpsessid, env_orgcode, acw_tc, aliyungf_tc, last_env
24         )
25 
26         return cookie
27     except Exception as err:
28         print('获取cookie失败:\n{0}'.format(err))

    2. Use cookie:

1 import requests
2 
3 def get_data():
4     cookie = login()
5     headers = {
6         "cookie": cookie
7     }
8     res = requests.get(url=get_data_url, headers=headers)
9     print(res.text)

Guess you like

Origin www.cnblogs.com/liuzhzhao/p/12114453.html
Recommended