annotate Tools/scripts/serve.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 2cfb18f1c293
children 8c2635afbfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59106
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
1 #!/usr/bin/env python
59120
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
2 '''
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
3 Small wsgiref based web server. Takes a path to serve from and an
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
4 optional port number (defaults to 8000), then tries to serve files.
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
5 Mime types are guessed from the file names, 404 errors are thrown
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
6 if the file is not found. Used for the make serve target in Doc.
e47a64f9cc77 Add some notes about Tools/scripts/serve.py.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 59106
diff changeset
7 '''
59106
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
8 import sys
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
9 import os
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
10 import mimetypes
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
11 from wsgiref import simple_server, util
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
12
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
13 def app(environ, respond):
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
14
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
15 fn = os.path.join(path, environ['PATH_INFO'][1:])
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
16 if '.' not in fn.split(os.path.sep)[-1]:
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
17 fn = os.path.join(fn, 'index.html')
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
18 type = mimetypes.guess_type(fn)[0]
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
19
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
20 if os.path.exists(fn):
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
21 respond('200 OK', [('Content-Type', type)])
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
22 return util.FileWrapper(open(fn))
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
23 else:
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
24 respond('404 Not Found', [('Content-Type', 'text/plain')])
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
25 return ['not found']
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
26
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
27 if __name__ == '__main__':
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
28 path = sys.argv[1]
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
29 port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
9ebbcb7a3989 Issue #8004: add a serve target to the Doc Makefile.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
30 httpd = simple_server.make_server('', port, app)
61969
2cfb18f1c293 Make the ctl-C shutdown of serve.py prettier.
R. David Murray <rdmurray@bitdance.com>
parents: 61303
diff changeset
31 print "Serving %s on port %s, control-C to stop" % (path, port)
2cfb18f1c293 Make the ctl-C shutdown of serve.py prettier.
R. David Murray <rdmurray@bitdance.com>
parents: 61303
diff changeset
32 try:
2cfb18f1c293 Make the ctl-C shutdown of serve.py prettier.
R. David Murray <rdmurray@bitdance.com>
parents: 61303
diff changeset
33 httpd.serve_forever()
2cfb18f1c293 Make the ctl-C shutdown of serve.py prettier.
R. David Murray <rdmurray@bitdance.com>
parents: 61303
diff changeset
34 except KeyboardInterrupt:
2cfb18f1c293 Make the ctl-C shutdown of serve.py prettier.
R. David Murray <rdmurray@bitdance.com>
parents: 61303
diff changeset
35 print "\b\bShutting down."