[Twisted-web] Using web2 to run a Cherrpy site

David Konerding dakoner at gmail.com
Tue Mar 20 09:55:59 CST 2007

> Which version of cherrypy are you using?  I did an easy_install
> cherrypy and got 3.0.1 but your code didn't work on it.  wsgiApp is
> somewhere else now or something.

OK, I've updated my test example to work with CherryPy 3.0.  This works a
bit better,
in fact, when I visit the root of the site I get my cherrypy application!
I'm not tied to CherryPy 2.2 (unless TurboGears requires it) this 'fixes'
most of my problem.
I'm still having a problem if I access some URLs under the root, but I have
yet to determine if it's going
to be a problem.

Here's a CherryPy 3.0.1 application that works with Twisted web2 using the
WSGI app support.

from twisted.internet import reactor
from twisted.application import service, strports
from twisted.web2 import server, channel
from twisted.web2 import log
from twisted.web2 import wsgi
from twisted.web2.wsgi import WSGIResource
import cherrypy

## Basic cherrypy index object that dumps the WSGI environment
class Root(object):
    def index(self):
        s = []
        for key in cherrypy.request.wsgi_environ:
            s.append("%s=%s<br/>" % (key, cherrypy.request.wsgi_environ

        return "".join(s)

## Create an instance of the root application and mount it at /
r = Root()

## Convert the Root application to a hostable WSGI application
cpwsgiapp = cherrypy.Application(Root(), '/')

## Convert the CherryPy WSGI app to a Twisted WSGI resource
wsgi = wsgi.WSGIResource(cpwsgiapp)

## Set up the cherrypy environment

## Start the cherrypy engine without starting a blocking server

## Setup default common access logging
res = log.LogWrapperResource(wsgi)

# Create twisted web2 site
site = server.Site(res)

## Launch the HTTP site at port 8080

## code path invoked by twistd
if __name__ == '__builtin__':
    application = service.Application("demo")
    s = strports.service('tcp:8080', channel.HTTPFactory(site))

## code path when run directly
if __name__ == '__main__':
    from twisted.python import log as pythonlog
    import sys
    reactor.listenTCP(8080, channel.HTTPFactory(site))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20070320/1043f724/attachment.htm

More information about the Twisted-web mailing list