gpt4 book ai didi

python-2.7 - 如何在 Python 2.7 web.py 上启用 HTTPS?

转载 作者:行者123 更新时间:2023-12-01 00:19:22 35 4
gpt4 key购买 nike

我在 Linux 机器上使用 Python 2.7.5 和 web.py (v0.38)。下面是我的最基本形式的代码 (webhooks.py)

#!/usr/bin/python

import web

urls = ('/.*','WebHooks')
app = web.application(urls, globals())

class WebHooks:
def POST(self):
raw_payload = web.data()
json_encode = json.loads(raw_payload)

if __name__ == '__main__':
app.run()
  1. 我执行 python webhooks.py 9999
  2. 它打开一个本地端口http://0.0.0.0:9999/

我的问题:我已阅读位于 here 的文档我很难过。有人能帮我打开 HTTPS URL 吗? https://0.0.0.0:9999/

我尝试过的

将以下内容添加到我的代码中进行测试:

response = app.request("/.*", https=True)

我会得到一个错误:AttributeError: 'module' object has no attribute 'request'

我用 pip install urllib.py 解决了这个问题,然后将 import urllib 添加到我的代码顶部,但我最终遇到了一堆错误:

Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/web/application.py", line 239, in process
return self.handle()
File "/usr/lib/python2.7/site-packages/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/lib/python2.7/site-packages/web/application.py", line 461, in _delegate
cls = fvars[f]
KeyError: u'WebHooks'

Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/web/application.py", line 239, in process
return self.handle()
File "/usr/lib/python2.7/site-packages/web/application.py", line 229, in handle
fn, args = self._match(self.mapping, web.ctx.path)
AttributeError: 'ThreadedDict' object has no attribute 'path'

最佳答案

您走错了路,但不用担心。您正在尝试的 response = app.request("/.*", https=True) 位与您的应用程序制作 https 请求有关,而不是 < em>处理一个https请求。

参见 http://webpy.org/cookbook/ssl

在内部,web.py 使用 CherryPyWSGIServer。要处理 https,您需要为服务器提供 ssl_certificate 和 ssl_key。非常简单,在调用 app.run() 之前添加几行:

if __name__ == '__main__':
from web.wsgiserver import CherryPyWSGIServer
ssl_cert = '/path-to-cert.crt'
ssl_key = '/path-to-cert.key'
CherryPyWSGIServer.ssl_certificate = ssl_cert
CherryPyWSGIServer.ssl_private_key = ssl_key
app.run()

当然,在完整的解决方案中,您可能需要 apache 或 nginx 来处理 https 部分,但以上内容非常适合小型应用程序和测试。

关于python-2.7 - 如何在 Python 2.7 web.py 上启用 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40492100/

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