在本地使用SSL运行Heroku Python示例应用的问题

6 投票
2 回答
1012 浏览
提问于 2025-04-17 03:15

我最开始遇到的问题和这个问题一样。根据其中一个回答的建议,可以通过让应用程序在没有SSL的情况下运行来避免这个特定的问题。但是,由于Facebook将在几天后(2011年10月1日)强制要求应用使用https,这似乎不是一个长久的解决办法。我最开始尝试在app.run中启用ssl(在exampleapp.py的第149行左右)。像这样:

app.run(host='0.0.0.0', port=port, ssl_context='adhoc')

第一次尝试时启动失败,提示缺少OpenSSL模块。我在网上找到了一些解决方法,选择了以下做法:

(myapp)$ pip install pyopenssl

现在启动时没有任何抱怨:

(myapp)$ foreman start
10:35:25 web.1     | started with pid 26934
10:35:26 web.1     |  * Running on https://0.0.0.0:5000/
10:35:26 web.1     |  * Restarting with reloader

但是当我尝试访问应用时:

10:35:31 web.1     | ----------------------------------------
10:35:31 web.1     | Exception happened during processing of request from ('127.0.0.1', 61118)
10:35:31 web.1     | Traceback (most recent call last):
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
10:35:31 web.1     |     self.process_request(request, client_address)
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 310, in process_request
10:35:31 web.1     |     self.finish_request(request, client_address)
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 323, in finish_request
10:35:31 web.1     |     self.RequestHandlerClass(request, client_address, self)
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 639, in __init__
10:35:31 web.1     |     self.handle()
10:35:31 web.1     |   File "path_to_myapp/lib/python2.7/site-packages/werkzeug/serving.py", line 189, in handle
10:35:31 web.1     |     return rv
10:35:31 web.1     | UnboundLocalError: local variable 'rv' referenced before assignment
10:35:31 web.1     | ----------------------------------------
10:35:31 web.1     | Unhandled exception in thread started by <function inner at 0x10139e050>
10:35:31 web.1     | Traceback (most recent call last):
10:35:31 web.1     |   File "path_to_myapp/lib/python2.7/site-packages/werkzeug/serving.py", line 599, in inner
10:35:31 web.1     |     passthrough_errors, ssl_context).serve_forever()
10:35:31 web.1     |   File "path_to_myapp/lib/python2.7/site-packages/werkzeug/serving.py", line 355, in serve_forever
10:35:31 web.1     |     HTTPServer.serve_forever(self)
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 227, in serve_forever
10:35:31 web.1     |     self._handle_request_noblock()
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 287, in _handle_request_noblock
10:35:31 web.1     |     self.shutdown_request(request)
10:35:31 web.1     |   File "/usr/local/Cellar/python/2.7.1/lib/python2.7/SocketServer.py", line 459, in shutdown_request
10:35:31 web.1     |     request.shutdown(socket.SHUT_WR)
10:35:31 web.1     | TypeError: shutdown() takes exactly 0 arguments (1 given)

该怎么办呢?是Python版本不对,还是我对其他基本概念理解错了?

2 个回答

1

要搞清楚为什么“rv”会出问题,得看看你本地的环境、脚本、安装的模块和werkzeug的代码,因为它说从0.6版本开始就支持SSL。

最简单的办法是在你的电脑上安装一个老牌的网页服务器apache,然后加上mod_ssl模块,生成SSL证书。最后,按照Flask的帮助页面的说明添加mod_wsgi,这样你的应用就能正常运行了。

注意:对于第一次安装的人来说,可能会觉得有点复杂,但一旦开始了,apache就像一头工作马,过去15年里对我来说非常可靠。

1

这是Werkzeug中的一个错误,已经被修复了。

你需要在requirements.txt文件中把Werkzeug的版本改成至少0.8.2,然后运行pip install -r requirements.txt来升级。

撰写回答