gpt4 book ai didi

python-2.7 - Flask webservice https调用导致ssl错误

转载 作者:行者123 更新时间:2023-12-04 22:39:29 24 4
gpt4 key购买 nike

我有基于 Flask 的 Web 服务,提供 REST 接口(interface)。

我们编写了从 flask.Flask 继承的 webservice 类,无法在此处提供完整的代码,但 webservice 应用程序调用看起来像,app.run(host=host, port=port, ssl_context='adhoc)
有了这个,当我尝试运行 GET 请求时,我收到 SSL 错误,

  File "/opt/debesys/build/x86-64/debug/python/tt/pyrate/test/test_juno_sim.py", line 1077, in test_main_page
resp = requests.get(self.url, headers=self.headers, verify=False)
File "/opt/debesys/ext/linux/x86-64/release/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/opt/debesys/ext/linux/x86-64/release/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/opt/debesys/ext/linux/x86-64/release/lib/python2.7/site-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/opt/debesys/ext/linux/x86-64/release/lib/python2.7/site-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/opt/debesys/ext/linux/x86-64/release/lib/python2.7/site-packages/requests/adapters.py", line 382, in send
raise SSLError(e, request=request)
SSLError: [Errno 1] _ssl.c:507: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

我也尝试过启动应用程序,例如, app.run(host=host, port=port, ssl_context=('cert.pem', 'key.pem'))还使用 urllib2 模块而不是 requests 模块来获取请求。

但得到同样的错误。如果我尝试在没有 ssl_context 的情况下启动应用程序并使用 http 则成功获得响应。

这里我使用 headers = {'Content-Type': 'application/json'}url = "https://127.0.0.1:{}".format(port)
[shrishinde@localhost debesys]$ wget https://127.0.0.1:47855/
--2020-02-19 13:03:25-- https://127.0.0.1:47855/
Connecting to 127.0.0.1:47855... connected.
OpenSSL: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Unable to establish SSL connection.
[shrishinde@localhost debesys]$ wget http://127.0.0.1:47855/
--2020-02-19 13:03:28-- http://127.0.0.1:47855/
Connecting to 127.0.0.1:47855... connected.
HTTP request sent, awaiting response... 200 OK
Length: 31 [text/html]
Saving to: “index.html”

100%[=====================================================================================================================================================================>] 31 --.-K/s in 0s

2020-02-19 13:03:28 (6.25 MB/s) - “index.html” saved [31/31]

[shrishinde@localhost debesys]$

更新

找到解决方案:
在继承 Flask 之后,我们重写了 Flask run 方法,并且使用了 WSGIServer 所以我应该使用 ssl_args 而不是 ssl_context

希望上述步骤将帮助用户诊断类似的问题,以便保留问题。

最佳答案

我可能知道发生了什么,即使您应该在堆栈跟踪旁边提供部分代码以更好地理解问题。

SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol



当 OpenSSL 从服务器收到错误的握手参数时,会产生此错误。如果服务器使用纯 HTTP(没有 TLS)进行响应,则可能会发生这种情况。它可能由于其他各种原因而发生,您的客户端可能不理解 TLS 版本(库仅支持 SSL)或类似的东西。

尝试使用 wget 运行请求或 Postman到网址 ( http://your.url:422)。如果您收到响应,则 SSL 未启用。

固定

好的,我已经设法复制了您的问题,您没有在 ssl 模式下运行服务器。运行这个。

pip install pyopenssl

python -m flask run --cert=adhoc



并确保消息在 https://127.0.0.1:5000/ 上运行然后退出它并运行它

python -m flask run --cert=adhoc -p [your port]



在所需端口中运行 int

关于python-2.7 - Flask webservice https调用导致ssl错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294853/

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