Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对不指向netrc的SSL站点的Python调用request()_Python_Ssl_Python Requests - Fatal编程技术网

对不指向netrc的SSL站点的Python调用request()

对不指向netrc的SSL站点的Python调用request(),python,ssl,python-requests,Python,Ssl,Python Requests,使用Anaconda3测试一段简单的代码: import requests as req resp = req.get("https://api.github.com") print(resp.text) 我得到了这个错误: SSLError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad hand

使用Anaconda3测试一段简单的代码:

import requests as req

resp = req.get("https://api.github.com")
print(resp.text)
我得到了这个错误:

SSLError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))
使用调试器跟踪调用,问题似乎发生在requests.utils中的
get\u netrc\u auth()
。这里,此代码用于获取netrc文件的完整路径:

        for f in NETRC_FILES:
            try:
                loc = os.path.expanduser('~/{}'.format(f))
然而,这只是给出了我的主目录的路径,而netrc位于工作环境的
Lib
目录中

显然,我的某个环境变量设置不正确。建议

编辑到问题:
根据Steffen的回答,netrc似乎不是问题所在。Marsiliou关于直接包括证书文件路径的建议(即。
resp=req.get(“https://api.github.com“验证='/path/to/certfile')
)是否有效。。。一旦昨天

我的代码现在如下所示:

import requests as req
from os import environ

cert_path = environ['CONDA_PREFIX'] + '\Lib\site-packages\certifi\cacert.pem'
print (cert_path)

resp = req.get("https://api.github.com", verify= cert_path)
print(resp.text)
cert\u path
扩展为
C:\ProgramData\Anaconda3\Lib\site packages\certifi\cacert.pem

这会导致相同的错误。是否有任何建议(除了将
验证
设置为
False


PS-以及回答Steffen的其他问题-这是Anaconda3,Python 3.7.3,在Windows 10上。

确保添加用户代理

import requests as req
# these import are for the verify false to avoid having a warning in the console
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
disable_warnings(InsecureRequestWarning)
disable_warnings(InsecureRequestWarning)

Headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
}
resp = req.get(url="https://api.github.com",headers=Headers, verify=False)
print(resp.text)

我想你的问题在这里得到了回答,你试过这个吗?

一旦正确设置了信任存储,代码就可以在没有任何特定用户代理的情况下完美运行。代码抱怨证书验证失败。在发送HTTP请求之前的TLS握手期间完成的证书验证是在任何用户代理设置生效之前完成的。@SteffenUllrich我知道这就是为什么我添加了verify false,但我遇到了一个类似的问题,用户代理是解决方案,所以我说不要忘记添加它以防万一证书没有问题我非常怀疑问题是从内部触发的
get\u netrc\u auth
。问题是信任存储的设置不正确,这意味着无法验证证书。不幸的是,您的系统(操作系统?、Anaconda3的确切版本?)一无所知,因此很难判断到底是什么错误。谢谢,Stefan。是的,你是对的;那个问题有点像是在转移视线。回答您的其他问题:Windows 10、Python 3.7.3。Marsilinou的回答确实解决了我的问题,但也提出了一个问题:既然
certifi
请求引用,
cacert.pem
certifi的一部分,
为什么必须在函数调用中显式引用它?谢谢!是的,这就是答案。但是,由于cacert.pem包含在certifi包中(该包在requests包中引用),它是否应该自动拉入?