[Twisted-web] twisted.web.wsgi usage example

Phil Christensen phil at bubblehouse.org
Tue Dec 9 14:40:13 EST 2008


Just because I've found myself doing this several times, you might  
give an example of how to run an existing WSGI application in twisted.

I would suggest Trac, since you can just import  
trac.web.main.dispatch_request and pass it as your WSGI application  
object, plus it's something that I know I often end up using in tandem  
with web projects.

-phil

On Dec 9, 2008, at 12:38 AM, Waldemar Osuch wrote:

> Hi,
> I'm trying to come up with an example on how to use the spanking new
> twisted.web.wsgi
> module.  Here it is.  Any comments?
>
> 8 
> < 
> --------------------------------------------------------------------------------------------
>
> from twisted.internet import reactor
> from twisted.application import service, internet
> from twisted.web import server, static, wsgi
> from twisted.python import threadpool
>
>
> def wsgi_app(environ, start_response):
>    status = '200 OK'
>    response_headers = [('Content-type', 'text/plain')]
>    start_response(status, response_headers)
>    return ['Hello from twisted.web.wsgi']
>
>
> class WSGIService(service.Service):
>    def __init__(self, minPoolSize=5, maxPoolSize=20):
>        self.pool = threadpool.ThreadPool(minPoolSize, maxPoolSize)
>
>    def startService(self):
>        self.pool.start()
>        service.Service.startService(self)
>
>    def stopService(self):
>        self.pool.stop()
>        service.Service.stopService(self)
>
>
> application = service.Application('wsgi')
>
> root = static.File("www/documents")
> site = server.Site(root)
> internet.TCPServer(8080, site).setServiceParent(application)
>
> serv = WSGIService()
> root.putChild('wsgi', wsgi.WSGIResource(reactor, serv.pool, wsgi_app))
> serv.setServiceParent(application)
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web




More information about the Twisted-web mailing list