【发布时间】:2020-02-19 07:13:43
【问题描述】:
我有提供 REST 接口的基于 Flask 的网络服务。
我们编写了继承自 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。
希望上述步骤能帮助用户诊断类似问题,因此请保留问题。
【问题讨论】:
标签: python-2.7 ssl flask python-requests