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

Todd Thomas caliban19 at gmail.com
Thu Feb 9 13:57:57 MST 2006


On 2/9/06, James Y Knight <foom at fuhm.net> wrote:
>
> > 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):
>    implements(IResource)
>
>    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
>
> James
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>
The final solution I came up with is..

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'

wsgi = wsgi.WSGIResource(WSGIHandler())

root = wsgi
application = service.Application("web")
site = server.Site(root)
s = strports.service('tcp:8000', http.HTTPFactory(site))
s.setServiceParent(application)

media = static.File("./myproject/media")
site2 = server.Site(media)
m = strports.service('tcp:8080', http.HTTPFactory(site2))
m.setServiceParent(application)

But what you show is awesome, been looking at how to do something like that.
Thanks for all the help, this issue is officially solved..

ToddB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20060209/45725fcd/attachment.htm


More information about the Twisted-web mailing list