Python requests的SSL證書驗證方式

requests的SSL證書驗證

1、對於HTTPS

默認情況下,啟用SSL驗證,如果無法驗證SSL證書會導致:

requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

解決辦法:

# 在requests中加入verify=False
res = requests.get(ur,verify=False)

2、以上步驟後,會出現警告

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)

解決辦法:

 from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全請求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

requests處理不信任的ssl證書

不驗證ssl證書

requests.get(url,verify=False)

以前的12306證書不被認證

對於https協議

會有一個ssl證書的加密認證

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: