Joc*_*zel 10 python twisted web-frameworks twisted.web
我有一个用Twisted编写的应用程序,我想添加一个Web界面来控制和监视它.我需要大量的动态页面来显示当前的状态和配置,所以我希望一个框架至少提供一个带继承和一些基本路由的模板语言.
因为我正在使用Twisted,所以我想使用twisted.web
- 但它的模板语言太基础了,似乎唯一的框架,Nevow已经死了(它在启动板上,但主页和wiki已关闭,我找不到任何文档) .
那么我的选择是什么?
twisted.web
基于框架?谢谢你的回答.
Joc*_*zel 14
由于Nevow仍在使用,我不想自己编写路由和支持模板库,我最终使用了Flask.结果很简单:
# make a Flask app
from flask import Flask, render_template, g
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
# run in under twisted through wsgi
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
resource = WSGIResource(reactor, reactor.getThreadPool(), app)
site = Site(resource)
# bind it etc
# ...
Run Code Online (Sandbox Code Playgroud)
到目前为止,它完美无缺.
您可以将其直接绑定到反应器中,如下例所示:
reactor.listenTCP(5050, site)
reactor.run()
Run Code Online (Sandbox Code Playgroud)
如果需要将子项添加到WSGI根目录,请访问此链接以获取更多详细信息.
这是一个示例,说明如何将WSGI资源与静态子组合.
from twisted.internet import reactor
from twisted.web import static as Static, server, twcgi, script, vhost
from twisted.web.resource import Resource
from twisted.web.wsgi import WSGIResource
from flask import Flask, g, request
class Root( Resource ):
"""Root resource that combines the two sites/entry points"""
WSGI = WSGIResource(reactor, reactor.getThreadPool(), app)
def getChild( self, child, request ):
# request.isLeaf = True
request.prepath.pop()
request.postpath.insert(0,child)
return self.WSGI
def render( self, request ):
"""Delegate to the WSGI resource"""
return self.WSGI.render( request )
def main():
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)static = Static.File("/path/folder") static.processors = {'.py': script.PythonScript, '.rpy': script.ResourceScript} static.indexNames = ['index.rpy', 'index.html', 'index.htm'] root = Root() root.putChild('static', static) reactor.listenTCP(5050, server.Site(root)) reactor.run()
归档时间: |
|
查看次数: |
6750 次 |
最近记录: |