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

David Konerding dakoner at gmail.com
Sun Mar 18 20:08:58 CST 2007


I am using web2 to run a cherrypy server at the root of the site using WSGI.

I've got a nearly working set up, the only problem is that when a request
comes in for the root of the site, it gets redirected
back to itself:
% curl http://localhost:8080
This resource resides temporarily at <a href='http://localhost:8080/'>

Intriguingly, if I request locations below the root, things work more OK:
% curl http://localhost:8080/test/index

then it works OK, even though I am registering the cherrypy at the root of
the site.

I've experimented, and if I register the cherrypy site in a subdir,
everything works normally as expected!

Here's the code that puts it at the root of the site.  I've tested this code
with a basic WSGI app,
and it works OK.  Only cherrypy seems to have this problem.  One clue is
that the logger shows
that the URL being requested is '//' instead of '/':

2007-03-18 19:07:02-0700 [HTTPChannel,6,] - -
[18/Mar/2007:19:07:02] "GET // HTTP/1.1" 302 97 "" "curl/7.15.1
(i686-suse-linux) libcurl/7.15.1 OpenSSL/0.9.8a zlib/1.2.3 libidn/0.6.0"

Here's the nearly working test example:

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
from cherrypy._cpwsgi import wsgiApp
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)

## Run this site using "twistd -noy"
if __name__ == '__builtin__':

    ## Create an instance of the root object and mount it at /
    r = Root()
    cherrypy.tree.mount(r, '/')
    wsgi = wsgi.WSGIResource(wsgiApp)

    ## Set up the cherrypy environment

    ## Start the cherrypy server; don't actually run an HTTP server, though
    cherrypy.server.start(initOnly=True, serverClass=None)

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

    # Create twisted web2 site
    site = server.Site(res)
    application = service.Application("demo")

    ## Launch the HTTP site at port 8080
    s = strports.service('tcp:8080', channel.HTTPFactory(site))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20070318/d9a9cd4f/attachment.htm

More information about the Twisted-web mailing list