Hello all,

For those wanting (like me) to serve static content without using the 
"static" dir (for instance, for a file manager application): web.py 
0.3 allows also generators to be returned from methods.

The following sample application can be used to serve static content 
from the "mystatic" dir:

####################################################################
import web, os, wsgiref.util, mimetypes, datetime

def serve_static(filename, mime_type=None):
  ''' Serves a file statically '''
  if mime_type is None:
    mime_type = mimetypes.guess_type(filename)[0]
  web.header('Content-Type', '%s' % mime_type)
  stat = os.stat(filename)
  web.header('Content-Length', '%s' % stat.st_size)
  web.header('Last-Modified', '%s' %  
web.http.lastmodified(datetime.datetime.fromtimestamp(stat.st_mtime)))
  return wsgiref.util.FileWrapper(open(filename, 'rb'), 16384)  

class Static(object):
  ''' Static file serving. Although '''
  def GET(self, name):
    return serve_static(os.path.join('mystatic', name))

urls = (
  '/mystatic/(.*)', Static
)
app = web.application(urls, globals())
if __name__ == "__main__": 
   app.run()
####################################################################

The serve_static() function accepts a mime_type parameter (otherwise 
it will try to guess), and adds the Content-Type, Content-Length and
and Last-Modified headers to the response.

Everybody, feel free to use the code above as wanted.
-- 
Best Regards,
Steve Howe

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to