gpt4 book ai didi

Python ssl/https requests.get 调用失败

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:16 25 4
gpt4 key购买 nike

我有以下 python (2.7.14) 片段,它在 requests.get 调用中抛出异常。它在 FreeBSD 服务器上运行,带有 apache 前端,所有 http 流量都被重定向到 https。请注意,我对 python 和这段代码都不太熟悉。在我的 PC 上本地运行且没有 https/ssl 时,以下内容也能正常工作。

urlstr = getApipath()  + 'getData/' + id_data
logging.debug("URL used: %s", urlstr)
try:
resp = requests.get(urlstr)
except Exception as e:
logging.exception("caught exception str: " + str(e))

urlstr 是一个 api 调用并使用 https。在同一主机的命令行上使用 curl 的相同 api 调用返回预期的有效 json。

curl https://my.domain.com/is/api/getData/D_01

以下是运行时的日志输出,包括堆栈跟踪(显然没有使用真实的 url)。看起来像一个 ssl 问题,但错误消息字符串为空没有帮助。非常感谢任何有关问题的帮助。

2018-03-28 10:37:51,530 URL used: https://my.domain.org/is/api/getData/D_01
2018-03-28 10:37:51,531 Starting new HTTPS connection (1): my.domain.org
2018-03-28 10:37:51,540 caught excpetion str: []
Traceback (most recent call last):
File "/var/www/engines/controller/get_data.py", line 25, in getTemplate
resp = requests.get(urlstr)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 850, in _validate_conn
conn.connect()
File "/usr/local/lib/python2.7/site-packages/urllib3/connection.py", line 314, in connect
cert_reqs=resolve_cert_reqs(self.cert_reqs),
File "/usr/local/lib/python2.7/site-packages/urllib3/util/ssl_.py", line 254, in create_urllib3_context
context = SSLContext(ssl_version or ssl.PROTOCOL_SSLv23)
File "/usr/local/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py", line 379, in __init__
self._ctx = OpenSSL.SSL.Context(self.protocol)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 724, in __init__
_openssl_assert(res == 1)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/_util.py", line 67, in openssl_assert
exception_from_error_queue(error)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
Error: []

最佳答案

升级到 python 加密模块解决了这个问题。安装的版本是 2.1.4。原来有一个更新的版本可用 - 2.2.2,安装后解决了问题。

关于Python ssl/https requests.get 调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com