发布时间:2019-05-02 18:19:24编辑:Run阅读(5760)
SSL证书验证
requests提供了证书验证的功能,当发送http请求的时候,它会检查SSL证书,使用verify参数控制是否检查此证书,如果不加verify参数的话,默认是True,会自动验证。
很多网站的证书没有被官方CA机构信任,会出现证书验证错误的结果,访问的时候可以看到一个证书问题的页面,如下图:
用requests来测试一下:
import requests response = requests.get('https://www.ebuy17.com/') print(response.status_code)
返回信息:
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.ebuy17.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError("hostname 'www.ebuy17.com' doesn't match either of 'www.nbxmsyj.com', 'nbxmsyj.com'")))
这里提示了一个错误SSLError,表示证书验证错误,如果请求一个HTTPS站点,但是证书验证错误的页面时,就会报这种错误,如何避免类似错误,很简单,把verify参数设置成False即可,代码如下:
import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } response = requests.get('https://www.ebuy17.com/', headers=headers, verify=False) print(response.status_code)
返回信息:
C:\python3.7.2\lib\site-packages\urllib3\connectionpool.py:847: 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)
200
出现了一个警告信息,它建议指定证书,可以通过设置忽略警告的方式来屏蔽这个警告。
import requests import urllib3 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } urllib3.disable_warnings() response = requests.get('https://www.ebuy17.com/', headers=headers, verify=False) print(response.status_code)
返回信息:
200
或者通过捕获警告日志的方式忽略警告,如下:
import requests import logging headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } logging.captureWarnings(True) response = requests.get('https://www.ebuy17.com/', headers=headers, verify=False) print(response.status_code)
返回信息:
200
也可以指定一个本地证书用作客户端证书,可以是单个文件(包含密钥和证书)或一个包含两个文件路径的元组,使用这种方法需要有crt和key文件,并且指定它们的路径,如下:
import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' } response = requests.get('https://www.ebuy17.com/', headers=headers, verify=False, cert=('/path/server.crt', '/path/key')) print(response.status_code)
返回信息:
200
47744
46234
37109
34626
29228
25885
24744
19862
19415
17907
5715°
6314°
5834°
5887°
6983°
5829°
5845°
6360°
6315°
7672°