Windows:Python SSL证书验证失败 [英] Windows: Python SSL certificate verify failed

查看:335
本文介绍了Windows:Python SSL证书验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Anaconda并尝试通过Jupyter笔记本进行API调用时遇到SSL问题:

I've installed Anaconda and had SSL problems when trying to do API calls via Jupyter Notebooks:

import requests
import certifi

r = requests.get('https://github.com/')
print(r)

这首先产生了SSL连接错误.经过广泛的搜索和我们IT部门的帮助,我可以解决该问题.解决方案是将公司根证书添加到证书存储中.

This first produced a SSL connection error. Which I could solve after extensive search and the help of our IT department. The solution here was to add the company root certificate to certifi cert storage.

不幸的是,现在对于其他请求,我仍然遇到相同的问题.使用google2pandas包调用Google Analytics API的示例代码:

Now for other requests unfortunately I still have the same problems. Example code calling the Google Analytics API with google2pandas package:

from google2pandas import *

query = {
    'reportRequests': [{
        'viewId' : 37616054,

        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

        'dimensions' : [
            {'name' : 'ga:date'}, 
            {'name' : 'ga:pagePath'},
            {'name' : 'ga:browser'}],

        'metrics'   : [
            {'expression' : 'ga:pageviews'}],

        'dimensionFilterClauses' : [{
            'operator' : 'AND',
            'filters'  : [
                {'dimensionName' : 'ga:browser',
                 'operator' : 'REGEXP',
                 'expressions' : ['Firefox']},

                {'dimensionName' : 'ga:pagePath',
                 'operator' : 'REGEXP',
                 'expressions' : ['iPhone']}]
        }]
    }]
}

# Assume we have placed our client_secrets_v4.json file in the current
# working directory.

conn = GoogleAnalyticsQueryV4(secrets='Analytics.json')
df = conn.execute_query(query)

在这里,我仍然收到之前在简单调用中遇到的SSL错误:

Here I still get the SSL error I had on the simple call before as well:

C:\ ProgramData \ Anaconda3 \ lib \ ssl.py在_create(cls,sock,server_side, do_handshake_on_connect,suppress_ragged_eofs,server_hostname, 上下文,会话) 848#无阻塞 849提高ValueError("do_handshake_on_connect不应该为 非阻塞插座") -> 850 self.do_handshake() 851除了(OSError,ValueError): 852 self.close()

C:\ProgramData\Anaconda3\lib\ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session) 848 # non-blocking 849 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") --> 850 self.do_handshake() 851 except (OSError, ValueError): 852 self.close()

do_handshake中的C:\ ProgramData \ Anaconda3 \ lib \ ssl.py(自我,阻止)
1106,如果超时== 0.0并阻止:1107
self.settimeout(无) -> 1108 self._sslobj.do_handshake()1109最后:1110 self.settimeout(timeout)

C:\ProgramData\Anaconda3\lib\ssl.py in do_handshake(self, block)
1106 if timeout == 0.0 and block: 1107
self.settimeout(None) -> 1108 self._sslobj.do_handshake() 1109 finally: 1110 self.settimeout(timeout)

SSLCertVerificationError:[SSL:CERTIFICATE_VERIFY_FAILED]证书 验证失败:无法获取本地发行者证书(_ssl.c:1045)

SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)

我相信还有一个不依赖证书的图书馆在使用中?但是我对在何处以及如何添加根证书一无所知,因此所有iPython请求都可以使用.

I believe there is another library in use, that doesn't rely on certifi? But I don't have any idea on where and how to add my root certificate, so all iPython requests will work.

任何想法都值得赞赏.

推荐答案

我花了几天时间弄清楚如何解决此问题.最后,我将我公司的CA证书添加到请求库使用的配置文件中.您可以通过以下方式检查此文件的路径:

I spent a few days figuring out how to solve this problem. Finally I add the CA certificate of my company in a configuration file used by requests library. You can check the path of this file by:

import requests as r
print(r.certs.where())

只需在证书的底部添加证书!

Just add the certificate at the bottom of it!

这篇关于Windows:Python SSL证书验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆