Use verify
parameters to ignore CA certificates
The CA certificates of some websites have not been certified by a trusted root certification authority, resulting in an SSL error message similar to 12306 websites appearing when using a browser to access. (12306 website before October 2018)
When using requests
the library to send a request, if the visited website has similar problems, ssl.CertificateError
an exception containing the words will be thrown.
Run the code to see the effect
You can run the following code to try to access a website with a CA certificate problem, and a certificate verification exception will be thrown:
# 导入requests模块,用于进行HTTP请求
import requests
# 要访问的URL
url = "https://sam.huat.edu.cn:8443/selfservice/"
try:
# 发起GET请求并获取响应
response = requests.get(url)
# 检查响应状态码是否为2xx(表示请求成功)
response.raise_for_status()
except requests.exceptions.RequestException as e:
# 如果发生异常,则打印异常信息
print("发生异常:", e)
solution
In order to make a normal request in the code, verify=False
parameters can be used. At this time, requests
the module will not verify the CA certificate when sending the request:
# 导入requests模块,用于进行HTTP请求
import requests
# 要访问的URL
url = "https://sam.huat.edu.cn:8443/selfservice/"
# 发起GET请求并获取响应,忽略SSL证书验证(verify=False)
response = requests.get(url, verify=False)
Note: By requests.get()
adding verify=False
parameters in the method, SSL certificate verification can be ignored. This may be useful in some cases, but be aware that it may be a security risk. Please use this parameter with caution and make sure you are visiting a trusted website.