blob: b8d9cd73387d9607340cec000c58520b13b3d92b [file] [log] [blame]
Dirkjan Ochtmane4c74e12010-02-24 04:12:11 +00001#!/usr/bin/env python
Dirkjan Ochtman60677a72010-02-24 17:06:31 +00002'''
3Small wsgiref based web server. Takes a path to serve from and an
4optional port number (defaults to 8000), then tries to serve files.
5Mime types are guessed from the file names, 404 errors are thrown
6if the file is not found. Used for the make serve target in Doc.
7'''
Dirkjan Ochtmane4c74e12010-02-24 04:12:11 +00008import sys
9import os
10import mimetypes
11from wsgiref import simple_server, util
12
13def app(environ, respond):
14
15 fn = os.path.join(path, environ['PATH_INFO'][1:])
16 if '.' not in fn.split(os.path.sep)[-1]:
17 fn = os.path.join(fn, 'index.html')
18 type = mimetypes.guess_type(fn)[0]
19
20 if os.path.exists(fn):
21 respond('200 OK', [('Content-Type', type)])
22 return util.FileWrapper(open(fn))
23 else:
24 respond('404 Not Found', [('Content-Type', 'text/plain')])
25 return ['not found']
26
27if __name__ == '__main__':
28 path = sys.argv[1]
29 port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
30 httpd = simple_server.make_server('', port, app)
R. David Murray1f7de712010-05-06 00:59:04 +000031 print "Serving %s on port %s" % (path, port)
Dirkjan Ochtmane4c74e12010-02-24 04:12:11 +000032 httpd.serve_forever()