Python请求。[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:645)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38065240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:16:34  来源:igfitidea点击:

Python Request. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

pythonssl-certificatepython-requests

提问by Anmol Bhatia

In my python script, i am trying to call my api as follows:

在我的 python 脚本中,我试图按如下方式调用我的 api:

with requests.Session() as session:
  url= 'https://example.com'
  headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
  }

request_parameters = {'username': 'test', 'password':'pass'}
response = (session.post(auth_url, data=request_parameters, headers=headers))

I am getting the following exception:

我收到以下异常:

 File "C:\Users\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 518, in post return self.request('POST', url, data=data, json=json, **kwargs)

File "C:\Users\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)

File "C:\Users\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 585, in send
r = adapter.send(request, **kwargs)

File "C:\Users\Programs\Python\Python36\lib\site-   packages\requests\adapters.py", line 477, in send

raise SSLError(e, request=request)

requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

I want to call my service as https://example.com. It works for http:// connection but does not work for https://. What should i do for https connection? Also, when i call the service through Rest Client as https it works perfectly. Is there any other way to call the service as https connection?

我想将我的服务称为https://example.com。它适用于 http:// 连接,但不适用于 https://。我应该为 https 连接做什么?此外,当我通过 Rest Client 作为 https 调用该服务时,它运行良好。有没有其他方法可以将服务称为 https 连接?

回答by Vasili Syrakis

You can disable SSL verification, but it is not recommended.

您可以禁用 SSL 验证,但不建议这样做。

If you know and trust the site that you are posting data to, it's okay, but be aware that if it was hiHymaned you would be posting data to something malicious until you realized it was compromised.

如果您知道并信任您发布数据的站点,那没问题,但请注意,如果它被劫持,您会将数据发布到恶意程序,直到您意识到它已被入侵。

with requests.Session() as session:
    session.post('https://google.com', data={'hello': 1}, verify=False)

This is the warning that you will receive when not using verification:

这是您在不使用验证时会收到的警告:

C:\Users\vsyrakis\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py:791: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.htmlInsecureRequestWarning)

C:\Users\vsyrakis\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\connectionpool.py:791: InsecureRequestWarning: 正在发出未经验证的 HTTPS 请求。强烈建议添加证书验证。请参阅:https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)