Web interface for a twisted application

后端 未结 3 1248
广开言路
广开言路 2020-12-28 22:25

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 conf

3条回答
  •  太阳男子
    2020-12-28 23:17

    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()
    

提交回复
热议问题