- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathserve.py
executable file
·35 lines (31 loc) · 1.12 KB
/
serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
'''
Small wsgiref based web server. Takes a path to serve from and an
optional port number (defaults to 8000), then tries to serve files.
Mime types are guessed from the file names, 404 errors are raised
if the file is not found. Used for the make serve target in Doc.
'''
importsys
importos
importmimetypes
fromwsgirefimportsimple_server, util
defapp(environ, respond):
fn=os.path.join(path, environ['PATH_INFO'][1:])
if'.'notinfn.split(os.path.sep)[-1]:
fn=os.path.join(fn, 'index.html')
type=mimetypes.guess_type(fn)[0]
ifos.path.exists(fn):
respond('200 OK', [('Content-Type', type)])
returnutil.FileWrapper(open(fn))
else:
respond('404 Not Found', [('Content-Type', 'text/plain')])
return ['not found']
if__name__=='__main__':
path=sys.argv[1]
port=int(sys.argv[2]) iflen(sys.argv) >2else8000
httpd=simple_server.make_server('', port, app)
print"Serving %s on port %s, control-C to stop"% (path, port)
try:
httpd.serve_forever()
exceptKeyboardInterrupt:
print"\b\bShutting down."