Mercurial > universal-identity-relyer
view static_file_serving.py @ 15:7a59f0eceee7 default tip
static_file_serving serves on port 8000 if it's run as a script now.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Fri, 18 Jun 2010 15:07:35 -0700 |
parents | 63ea847bfa75 |
children |
line wrap: on
line source
#! /usr/bin/env python import mimetypes import os import wsgiref.util class StaticFileApp(object): def __init__(self, root_dir): self.root_dir = root_dir def __call__(self, environ, start_response): path = environ['PATH_INFO'] def error_404(): start_response('404 Not Found', [('Content-Type', 'text/plain')]) return ['Not Found: %s' % path] if path == '/': path = '/index.html' if path.startswith('/') and environ['REQUEST_METHOD'] == 'GET': filename = path.split('/')[1] if filename in os.listdir(self.root_dir): mimetype, enc = mimetypes.guess_type(filename) f = open(os.path.join(self.root_dir, filename)) start_response('200 OK', [('Content-Type', mimetype)]) return wsgiref.util.FileWrapper(f) else: return error_404() return error_404() if __name__ == '__main__': from wsgiref.simple_server import make_server dirname = os.getcwd() port = 8000 httpd = make_server('', port, StaticFileApp(dirname)) print "Serving files on port %d at %s." % (port, dirname) httpd.serve_forever()