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

时间:2011-09-28 08:54:22

标签: python facebook heroku openssl virtualenv

我开始遇到与this question相同的问题。正如其中一个答案所示,通过使应用程序在没有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 个答案:

答案 0 :(得分:1)

很难确定为什么“rv”在没有检查本地环境,脚本,已安装模块和werkzeug代码的情况下失败,这些代码声称从v0.6开始支持SSL。

最简单的解决方案是在本地计算机上安装Web服务器的爷爷apache,添加mod_ssl模块并生成这些SSL证书。最后,按照Flask help page中的说明添加mod_wsgi,以启动并运行您的应用。

注意:对于初学者来说,安装可能会令人生畏,但是一旦你开始,apache就是一个主力,并且在过去的15年里对我来说非常可靠。

答案 1 :(得分:1)

这是Werkzeug的一个错误,得到fixed

requirements.txt 文件中的Werkzeug版本更改为至少0.8.2并运行pip install -r requirements.txt进行升级。