python requests请求SSL证书问题

有价值的内容收集
回复
adm1n
Site Admin
帖子: 137
注册时间: 周六 6月 28, 2014 10:03 pm

python requests请求SSL证书问题

帖子 adm1n »

requests发送https请求时默认验证ssl证书,此时如果访问的地址ssl证书过期失效或不信任,则请求失败,为了使请求成功,可以设置verify为False,暂时不验证

代码: 全选

r = requests.get(url, headers=header, verify=False)
移除ssl验证后,可以访问,但会出现一长串提示:

代码: 全选

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
  
根据提示:https://urllib3.readthedocs.io/en/lates ... l-warnings
可以看到解决办法,可用disable_warnings()移除警告

代码: 全选

#在requests请求前添加
requests.packages.urllib3.disable_warnings()
完整代码:

代码: 全选

import requests

requests.packages.urllib3.disable_warnings()
r = requests.get(url, headers=header, verify=False)
文章来源:
https://www.cnblogs.com/linmo/p/14319808.html
回复