On Wed, 6 Aug 2008 16:40:49 -0500, Govind Salinas <[EMAIL PROTECTED]> wrote:
[snip]

I had tried using Django for what I was doing but I did not sure if it
would end up as a nice fit.  When I started using twisted, I borrowed
the regex->page mapping from them.  This is how I implemented it:

[snip]

   def render_GET(self, request):
       path = request.path[1:]
       uri = request.uri[1:]
       for mapping, f in self._mappings:
           match = mapping.match(uri)
           if match:
               vars = match.groupdict()
               print 'serving:', f
               lookup = TemplateLookup(directories=['/'])
               return Template(filename=f, lookup=lookup).render(**vars)
       path = os.path.join(self._template_dir, path)
       if os.path.isfile(path):

Note that if request.uri is something like "//etc/passwd", your server
might serve up some content you'd rather not see made public.  One
advantage of twisted.web.static.File is that you can specify a directory
and it will only allow things inside that directory to be served.

           f = None
           try:
               idx = path.rfind('.')
               print 'serving static:', path, path[idx + 1:]
               type = _mime_types.get(path[idx + 1:], 'text/plain')
               request.setHeader('content-type', type)
               print request.headers, type
               f = file(path, 'r')
               return f.read()
           except:
               print 'failed to serve:', path
               raise
           finally:
               if f:
                   f.close()

Another advantage of File is that it won't load the entire file into
memory all at once, it will set the content-type for you, and it will
generate 404s if necessary.

Jean-Paul

_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

Reply via email to