在python中使用请求时无法获取本地颁发者证书

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51925384/
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 19:58:47  来源:igfitidea点击:

Unable to get local issuer certificate when using requests in python

pythonpython-requests

提问by innocentDrifter

here is my code

这是我的代码

import requests;
url='that website';
headers={
  'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
};
r = requests.get(url,headers=headers);
print(r);
print(r.status_code);

then it ran into this:

然后它遇到了这个:

requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxxxxx.com', port=44 3): Max retries exceeded with url: xxxxxxxx (Caused by SSLEr ror(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)')))

requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxxxxx.com', port=44 3): Max retries exceeded with url: xxxxxxxx (Caused by SSLEr ror(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed) : 无法获得本地颁发者证书 (_ssl.c:1045)')))

what should i do?

我该怎么办?

回答by Indranil

It's not recommendedto use verify = Falsein your organization's environments. This is essentially disabling SSL verification.

它不建议使用verify = False您的组织环境。这实质上是禁用 SSL 验证。

Sometimes, when you are behind a company proxy, it replaces the certificate chain with the ones of Proxy. Adding the certificates in cacert.pem used by certifi should solve the issue. I had similar issue. Here is what I did, to resolve the issue -

有时,当您使用公司代理时,它会用代理的证书链替换证书链。在 certifi 使用的 cacert.pem 中添加证书应该可以解决问题。我有类似的问题。这是我所做的,以解决问题-

  1. Find the path where cacert.pem is located -
  1. 找到cacert.pem所在的路径——

Install certifi, if you don't have. Command: pip install certifi

如果没有,请安装证书。命令:pip install certifi

import certifi
certifi.where()
C:\Users\[UserID]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\certifi\cacert.pem
  1. Open the URL on a browser. Download the chain of certificates from the URL and save as Base64 encoded .cer files.

  2. Now open the cacert.pem in a notepad and just add every downloaded certificate contents (---Begin Certificate--- *** ---End Certificate---) at the end.

  1. 在浏览器上打开 URL。从 URL 下载证书链并另存为 Base64 编码的 .cer 文件。

  2. 现在在记事本中打开 cacert.pem 并在最后添加每个下载的证书内容 ( ---Begin Certificate--- *** ---End Certificate---)。

回答by Anastasiya Mazheika

In my case it was a running Fiddler...

就我而言,它是一个正在运行的 Fiddler ......

回答by pwxcoo

quickly fix:

快速修复:

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

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

Hope helpful.

希望有帮助。