Python+Requests module session processing and SSL certificate processing & closing warnings

session processing

Some interfaces need to log in to the URL before they have permission to call. At this time, sessions can be used. The specific operation is: first use the login API of the website to log in, get the session, and then use the session to request other interfaces.

Sample code:

session_obj = requests.session() # Used to keep the session connection, subsequent requests are sent with the session object

import requests,re 
# 2. Complete the phpwind forum interface 
# 1. Open the forum 
# Create a session object 
session_obj = requests.session() # Used to maintain the session connection, and subsequent requests are sent with the session object 
host = "http:// 47.107.178.45/" 
response = session_obj.get(url=host+"phpwind/") 
body = response.content.decode("utf-8") 
token = re.findall('csrf_token" value="(.+?) "/>',body)[0] 
print(token)

Certificate processing (SSL Cert Verification)

Many websites are https, but they can be accessed without a certificate. In most cases, certificates can be carried or not. For example, Zhihu\Baidu, etc. can be carried or not, but if there are rigid requirements, they must be carried, such as for orientation Only users who have obtained the certificate can access a specific website.

close warning

import requests,warnings
from requests.packages import urllib3
# 关闭警告
urllib3.disable_warnings()
warnings.filterwarnings("ignore")

Troubleshoot certificate issues

1. Close the certificate

Code example:

# 1, Close the certificate 
res = requests.get(url="https://www.12306.cn", verify=False) #Do not verify the certificate, report a warning, return 200 
print(res.content.decode("utf- 8"))

2. Installing the pyopenssl module can verify the certificate, but you can use verify=False in the request

Code example:

# 2, install the pyopenssl module to verify the certificate, you can not use verify=False in the request 
res = requests.get(url="https://www.12306.cn") 
print(res.content.decode("utf-8 "))

3. Add a certificate (it is recommended to find a company developer to ask for a .crt file)

Code example:

# 3, add the certificate, find the company developers to ask for the .crt file recommended 
res = requests.get(url="https://www.12306.cn",cert=("/path/certificate.crt", "/ path/key"))

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! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/130184060