【问题标题】:Python SSLError - Any way to Hide WarningPython SSLError - 隐藏警告的任何方式
【发布时间】:2018-10-20 09:02:21
【问题描述】:

我在 windows 和 python3 上工作。 我使用 requests 模块通过此代码访问网页 -

requests.get('https://github.com/')

然后我收到SSLError [SSL: CERTIFICATE VERIFY FAILED ] 错误。然后我用verify=False关闭了SSL证书检查的开关。

import requests
requests.get('https://github.com/', verify=False)

现在它正在浏览网站但返回警告。

Warning (from warnings module):
  File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 847
    InsecureRequestWarning)
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
<Response [200]>

现在我正在使用脚本,我不想向用户显示此警告。我有办法隐藏警告,但我不知道如何使用它。

warnings.simplefilter("ignore")

但这会忽略所有警告,我只想特别隐藏这个“InsecureRequestWarning”。请指导我如何做到这一点。

【问题讨论】:

    标签: python-3.x request warnings


    【解决方案1】:

    所以我得到了隐藏警告的解决方案。这就是我所做的。

    import warnings
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    
    warnings.simplefilter('ignore',InsecureRequestWarning)
    requests.get('https://github.com/',verify=False)
    

    而这件事解决了我的问题。

    【讨论】:

    • 谢谢,这与最近的请求包完美配合。
    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 2018-06-26
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多