扭曲应用程序的 Web 界面

发布于 2024-10-20 23:12:35 字数 466 浏览 7 评论 0原文

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

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

那么我有什么选择呢?

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

感谢您的回答。

I have a application written in Twisted and I want to add a web interface to control and monitor it. I'll need plenty of dynamic pages that show the current status and configuration, so I hoped for a framework that offers at least a templating language with inheritance and some basic routing.

Since I am using Twisted anyways I wanted to use twisted.web - but it's templating language is too basic and it seems that the only framework, Nevow is quite dead (it's on launchpad but the homepage and wiki are down and I can't find any documentation).

So what are my options?

  • Is there any other twisted.web based framework?
  • Are there other frameworks that work with twisted's reactor?
  • Should I just get a web framework (I'm thinking web.py or flask) and run it in a thread?

Thanks for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-10-27 23:12:35

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

到目前为止它工作得完美无缺。

Since Nevow is still down and I didn't want to write routing and support for a templating lib myself, I ended up using Flask. It turned out to be quite easy:

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

It works flawlessly so far.

逆光下的微笑 2024-10-27 23:12:35

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

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

如果您需要将子级添加到 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():
static = Static.File("/路径/文件夹")
static.processors = {'.py': script.PythonScript,
                 '.rpy':脚本.ResourceScript}
static.indexNames = ['index.rpy', 'index.html', 'index.htm']

根 = 根()
root.putChild('静态', 静态)

reactor.listenTCP(5050, server.Site(root))
反应堆.run()

You can bind it directly into the reactor like the example below:

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

If you need to add children to a WSGI root visit this link for more details.

Here is an example showing how to combine WSGI Resource with a static child.

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()
兔小萌 2024-10-27 23:12:35

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

您还可以使用twisted.web 基本上任何现有的模板模块;我想到了 Jinja2。

Nevow is the obvious choice. Unfortunately the divmod web server hardware and the backup server hardware failed at the same time. They are attempting to recover the data and publish it on launchpad, but it may take a while.

You could also use basically any existing template module with twisted.web; Jinja2 comes to mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文