[接口测试_B] 10 requests处理SSL证书验证

2018-04-22  本文已影响78人  乐大爷L

requests支持的证书验证方式

SSL证书验证

来个大写的问题,SSL是什么,为什么在发起HTTPS请求的时候需要证书验证?

Requests 为 HTTPS 请求验证 SSL 证书

>>> import requests
>>> requests.get('https://github.com')
requests.exceptions.SSLError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)'),))
>>> requests.get('https://github.com', verify=False)
D:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py:858: 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)
<Response [200]>

可以看到,将verify设为False后,可以跳过ssl验证,但存在一个告警信息InsecureRequestWarning。要忽略这个告警信息可以怎么做呢?

import requests
from requests.packages import urllib3

# 关闭告警信息
urllib3.disable_warnings()

url = 'https://github.com'
r = requests.get(url, verify=False)
print(r.status_code)

客户端证书验证方式

image.png

总结

上一篇下一篇

猜你喜欢

热点阅读