Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web interface for a twisted application

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.

like image 847
Jochen Ritzel Avatar asked Mar 09 '11 16:03

Jochen Ritzel


People also ask

What is twisted web?

Twisted Web is a web application server written in pure Python, with APIs at multiple levels of abstraction to facilitate different kinds of web programming.

What is twisted application?

The Twisted Application infrastructure takes care of running and stopping your application. Using this infrastructure frees you from from having to write a large amount of boilerplate code by hooking your application into existing tools that manage daemonization, logging, choosing a reactor and more.

What is twisted framework?

Twisted is an event-based framework for internet applications, supporting Python 3.6+. It includes modules for many different purposes, including the following: twisted. web: HTTP clients and servers, HTML templating, and a WSGI server. twisted.


3 Answers

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.

like image 170
Jochen Ritzel Avatar answered Sep 19 '22 11:09

Jochen Ritzel


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()
like image 42
Daniel Rocha Avatar answered Sep 23 '22 11:09

Daniel Rocha


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.

like image 37
nosklo Avatar answered Sep 22 '22 11:09

nosklo