使用 SSL 在本地运行适用于 Python 的 Heroku 示例应用程序时出现问题

发布于 2024-12-06 15:41:50 字数 3248 浏览 0 评论 0原文

我一开始遇到了与 这个问题。正如答案之一表明,可以通过使应用程序在没有 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 模块。找到了一些关于如何使用 ssl 的建议。在网上解决这个问题,并选择这样做:

(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版本还是只是我误解了其他一些基本的事情?

I started out with having the same problem as in this question. As one of the answers suggests one can avoid that particular problem by making the app run without SSL. But as Facebook is set to enforce https for apps in just a few days (October 1 2011) that seems like a solution that won't last. I first tried to enable ssl in app.run (around line 149 in exampleapp.py. Like so:

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

At first try it failed at start complaining about the missing OpenSSL module. Found a couple of suggestions on how to fix that out there on the net, and choose to do:

(myapp)$ pip install pyopenssl

Now no complaints at start:

(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

But then when trying to access the app:

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)

What to do about this? Is it the wrong Python version or just me missunderstanding some other fundamental thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-12-13 15:41:50

这是 Werkzeug 中的一个错误,已修复

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

It is a bug in Werkzeug, that got fixed.

Change the Werkzeug version in the requirements.txt file to at least 0.8.2 and run pip install -r requirements.txt to upgrade.

送舟行 2024-12-13 15:41:50

如果不检查本地环境、脚本、安装的模块和 werkzeug 代码,很难确定“rv”失败的原因,该代码声称从 v0.6 开始支持 SSL。

最简单的解决方案是在本地计算机上安装Web服务器的祖父apache,添加mod_ssl 模块并生成这些 SSL 证书。最后,按照 Flask 的帮助页面中所述添加 mod_wsgi 以使您的应用程序启动并运行。

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

Difficult to ascertain why "rv" fails without inspecting the local environment, scripts, installed modules and the werkzeug code, which claims SSL is supported from v0.6 onwards.

The easiest solution is to install the grandpa of web servers apache on your local machine, add in mod_ssl module and generate those SSL certs. Lastly, add mod_wsgi as described in Flask's help page to get your app up and running.

Note: installation may be daunting for first timers, but once you get going, apache is a workhorse and been extremely reliable to me over the past 15 yrs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文