作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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()
python webhooks.py 9999
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/
我是一名优秀的程序员,十分优秀!