简体   繁体   English

SSL:CERTIFICATE_VERIFY_FAILED 证书验证在 Python 中失败

[英]SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed in Python

While writing POST REQUEST in Python, I've faced some issue:在用 Python 编写 POST REQUEST 时,我遇到了一些问题:

self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1108)

I don't want to set: verify = False in the REQUESTS.我不想在请求中设置: verify = False

How I can handle this problem?我该如何处理这个问题?

The error happens because the certificate being used by the server was not issued by a certificate authority (CA) included in the default list of trusted CAs used by the requests module.发生错误是因为服务器使用的证书不是由requests模块使用的受信任 CA 的默认列表中包含的证书颁发机构 (CA) 颁发的。 It is a self-signed certificate, so you either need to tell requests explicitly to trust that individual cert, or (preferably, if the server is under your control), get a certificate signed by one of the trusted CAs and make the server use that instead.它是一个自签名证书,因此您要么需要明确告诉requests信任该个人证书,要么(最好,如果服务器在您的控制之下),获得由受信任的 CA 之一签名的证书并让服务器使用那个。

To trust only the exact certificate being used by the server, download it and instead of setting verify=False , set verify="/path/to/cert.pem" , where cert.pem is the server certificate.要仅信任服务器使用的确切证书,请下载它,而不是设置verify=False ,设置verify="/path/to/cert.pem" ,其中cert.pem是服务器证书。

Here are some more detailed instructions on creating the correct .pem file:以下是有关创建正确 .pem 文件的一些更详细的说明:

The following URL has instructions for downloading SSL certificates from a website using various browsers. 以下 URL包含使用各种浏览器从网站下载 SSL 证书的说明。 You need to create a certificate-chain .pem file and for that you need to use Firefox.您需要创建一个证书链.pem文件,为此您需要使用 Firefox。 We will pretend that google.com was the website with which you were having difficulty.我们会假设 google.com 是您遇到问题的网站。 When you get to the Certificate page, you will see something like the following:当您进入证书页面时,您将看到类似以下内容:

在此处输入图片说明

In this example you can chose either GTS CA 101 or GlobalSIGN and then click on the PEM (chain) download link.在本例中,您可以选择 GTS CA 101 或 GlobalSIGN,然后单击PEM (chain)下载链接。 This will create a file google-com-chain.pem in the directory of your choice.这将在您选择的目录中创建一个文件google-com-chain.pem

Then wherever the source specified verify=False , replace it with `verify='/path-to/google-com-chain.pem'然后在源指定的任何地方verify=False ,将其替换为 `verify='/path-to/google-com-chain.pem'

import request进口请求
response = requests.get("url/api that you want to hit", verify="path to ssl certificate") response = requests.get("你要点击的url/api", verify="ssl证书的路径")

For me the problem was that none of the above answers completely helped me but gave me the right direction to look at.对我来说,问题是上述答案都没有完全帮助我,但给了我正确的方向。

For sure, SSL certificate is needed but when you are behind the company's firewall then publicly available certificates might not help .当然,需要 SSL 证书,但是当您位于公司的防火墙后面时,公开可用的证书可能无济于事 You might need to reach out to the IT department of your company to obtain the certificate as each company uses special certificate from the security provider they have contracted the services from.您可能需要联系您公司的 IT 部门以获取证书,因为每家公司都使用与他们签订服务合同的安全提供商提供的特殊证书 And place it in a folder and pass the path to that folder as an argument to verify parameter.并将其放在一个文件夹中,并将该文件夹的路径作为参数传递给验证参数。

For me even after trying all the above solutions and using the wrong certificate I was not able to make it work.对我来说,即使在尝试了上述所有解决方案并使用了错误的证书后,我也无法使其正常工作。 So just remember for those who are behind company's firewall to obtain the right certificate.所以请记住,那些在公司防火墙后面的人要获得正确的证书。 It can make a difference between success and failure of your request call.它可以在您的请求调用的成功和失败之间产生差异。

In my case I placed the certificate in the following path and it worked like magic.在我的情况下,我将证书放在以下路径中,它就像魔术一样工作。

C:\\Program Files\\Common Files\\ssl C:\\Program Files\\Common Files\\ssl

You could also refer https://2.python-requests.org/en/master/user/advanced/#id3 which talks about ssl verification您还可以参考https://2.python-requests.org/en/master/user/advanced/#id3 ,其中讨论了 ssl 验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM