Python SSL证书验证问题解决方案


在requests访问https的站点时,如:

url = 'https://bigdata.hddly.cn/wp-content/plugins/captcha-code-authentication/captcha_code_file.php'
rqg = requests.get(url, headers=headers, proxies=proxies)

会报错:

raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='bigdata.hddly.cn', port=443): Max retries exceeded with url: /wp-content/plugins/captcha-code-authentication/captcha_code_file.php (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)')))

此时,可以使用verify=False参数,直接访问https: verify参数设置 1、Requests的请求默认verify=True 2、如果你将 verify设置为 False,Requests 也能忽略对 SSL 证书的验证 3、但是依然会出现两行Warning,可以不用管 例如:

rqg = requests.get(url,headers=headers,verify=False,proxies=proxies)

会有告警:

D:\app\python3\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host 'bigdata.hddly.cn'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(

Python3 提示warning 添加如下三种任意一种代码即可解决:

方式1:

import requests
requests.packages.urllib3.disable_warnings

方式2:

import warnings
warnings.filterwarnings("ignore")

方式3:

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

以上方式2和方式3,经验证可行。