10

我有一个用 Twisted 编写的应用程序,我想添加一个 Web 界面来控制和监视它。我需要大量显示当前状态和配置的动态页面,所以我希望有一个框架至少提供一种具有继承和一些基本路由的模板语言。

因为我无论如何都想使用 Twisted twisted.web- 但它的模板语言太基础了,而且似乎唯一的框架,Nevow 已经死了(它在启动板上,但主页和 wiki 已关闭,我找不到任何文档) .

那么我的选择是什么?

  • 有没有其他twisted.web的基础框架?
  • 是否有其他框架可以与扭曲的反应器一起使用?
  • 我是否应该只获得一个 Web 框架(我在想 web.py 或烧瓶)并在线程中运行它?

感谢您的回答。

4

3 回答 3

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
# ...

到目前为止,它完美无缺。

于 2011-03-15T15:32:18.877 回答
6

您可以将其直接绑定到反应器中,如下例所示:

reactor.listenTCP(5050, site)
reactor.run()

如果您需要将子级添加到 WSGI 根目录,请访问此链接以获取更多详细信息。

这是一个示例,展示了如何将 WSGI Resource 与静态子级结合起来。

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():
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()
于 2011-06-23T00:05:21.617 回答
3

新是显而易见的选择。不幸的是,divmod Web 服务器硬件备份服务器硬件同时出现故障。他们正在尝试恢复数据并将其发布到启动板上,但这可能需要一段时间。

你也可以使用任何现有的带有 twisted.web 的模板模块;Jinja2 浮现在脑海中。

于 2011-03-09T16:33:35.587 回答