[Twisted-web] Web2 Script.py seems to be broken

James Y Knight foom at fuhm.net
Thu Feb 9 11:52:38 MST 2006

> from twisted.application import strports, service
> from twisted.web2 import static, server, http, wsgi, resource
> import sys
> sys.path.append("./myproject")
> from django.core.handlers.wsgi import WSGIHandler
> import os
> os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
> class toplevel(wsgi.WSGIResource):
>     def child_media(self, ctx):
>         return static.File("./myproject/media")
> root = toplevel(WSGIHandler())
> application = service.Application("web")
> site = server.Site(root)
> s = strports.service('tcp:8000', http.HTTPFactory(site))
> s.setServiceParent(application)

That won't work -- what you want is to nest the resources rather than  
subclass them. Something like this resource should do you (untested,  
wrote in email client). It says: if the path starts with media,  
return the static File and pass it the remaining pieces of the path,  
otherwise pass the entire path onto wsgi for its processing.

class toplevel(object):

   def __init__(self):
     self.wsgi = wsgi.WSGIResource(WSGIHandler())
     self.media = static.File("./myproject/media"

   def locateChild(self, req, segs):
     if segs[0] == 'media':
       return self.media, segs[1:]
     return self.wsgi, segs

   def renderHTTP(self, req):
     return self.wsgi


More information about the Twisted-web mailing list