【问题标题】:Web interface for a twisted application扭曲应用程序的 Web 界面
【发布时间】:2011-03-09 16:26:55
【问题描述】:

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

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

那么我的选择是什么?

  • 还有其他基于twisted.web 的框架吗?
  • 还有其他框架可以与twisted 的reactor 一起使用吗?
  • 我是否应该只获取一个 Web 框架(我在考虑 web.py 或 flask)并在线程中运行它?

感谢您的回答。

【问题讨论】:

  • @Jean-Paul Calderone - 我相信 Nevow 不错;我对所有 divmod 项目印象深刻。但是随着 divmod.org 的关闭,初学者很难找到文档。

标签: python twisted web-frameworks twisted.web


【解决方案1】:

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

到目前为止,它运行良好。

【讨论】:

  • 请您详细说明bind it etc好吗?像这样,我们将有一个完全包含的、可运行的示例……对于那些不那么精通扭曲的人:)
  • @exhuma reactor.listenTCP(80, site) 会将烧瓶应用绑定到端口 80
  • 现在已经两年了。 Flask 和 Twisted 还活着,而且我正在考虑做类似的事情。这最终对您有用吗,还是您遇到了一些问题?
  • 这样运行时inlineCallbacks可以和Flask混用吗?
【解决方案2】:

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

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

如果您需要将子级添加到 WSGI 根目录,请访问 this link 了解更多详情。

这是一个示例,展示了如何将 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():
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()

【讨论】:

    【解决方案3】:

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

    您也可以使用任何现有的带有 twisted.web 的模板模块;想到 Jinja2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多