annotate Lib/SimpleHTTPServer.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 8cdb95cf096e
children a58b620e4dc9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Simple HTTP Server.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 This module builds on BaseHTTPServer by implementing the standard GET
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 and HEAD requests in a fairly straightforward manner.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
9 __version__ = "0.6"
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15865
diff changeset
11 __all__ = ["SimpleHTTPRequestHandler"]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 import os
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import posixpath
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import BaseHTTPServer
11475
c9cd4bbed8e2 Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents: 10816
diff changeset
16 import urllib
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
17 import cgi
68640
e9724d7abbc2 Fix issue11442 - Add a charset parameter to the Content-type to avoid XSS attacks.
Senthil Kumaran <orsenthil@gmail.com>
parents: 40513
diff changeset
18 import sys
13740
98d8169563e7 Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents: 12724
diff changeset
19 import shutil
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
20 import mimetypes
34293
cd517701fd93 Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents: 33214
diff changeset
21 try:
cd517701fd93 Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents: 33214
diff changeset
22 from cStringIO import StringIO
cd517701fd93 Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents: 33214
diff changeset
23 except ImportError:
cd517701fd93 Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents: 33214
diff changeset
24 from StringIO import StringIO
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 """Simple HTTP request handler with GET and HEAD commands.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 This serves files from the current directory and any of its
33014
0beb756965df [Bug #866222] Update docstrings.
Andrew M. Kuchling <amk@amk.ca>
parents: 30698
diff changeset
32 subdirectories. The MIME type for files is determined by
0beb756965df [Bug #866222] Update docstrings.
Andrew M. Kuchling <amk@amk.ca>
parents: 30698
diff changeset
33 calling the .guess_type() method.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 The GET and HEAD requests are identical except that the HEAD
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 request omits the actual contents of the file.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 server_version = "SimpleHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def do_GET(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
43 """Serve a GET request."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
44 f = self.send_head()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
45 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
46 self.copyfile(f, self.wfile)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
47 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 def do_HEAD(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
50 """Serve a HEAD request."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
51 f = self.send_head()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
52 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
53 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 def send_head(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
56 """Common code for GET and HEAD commands.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
58 This sends the response code and MIME headers.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
60 Return value is either a file object (which has to be copied
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
61 to the outputfile by the caller unless the command was HEAD,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
62 and must be closed by the caller under all circumstances), or
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
63 None, in which case the caller has nothing further to do.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
65 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
66 path = self.translate_path(self.path)
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
67 f = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
68 if os.path.isdir(path):
40513
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
69 if not self.path.endswith('/'):
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
70 # redirect browser - doing basically what apache does
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
71 self.send_response(301)
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
72 self.send_header("Location", self.path + "/")
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
73 self.end_headers()
09f4b460baeb [Patch #827559 from Chris Gonnerman] Make SimpleHTTPServer redirect when a directory URL is missing the trailing slash; this lets relative links work.
Andrew M. Kuchling <amk@amk.ca>
parents: 38834
diff changeset
74 return None
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
75 for index in "index.html", "index.htm":
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
76 index = os.path.join(path, index)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
77 if os.path.exists(index):
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
78 path = index
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
79 break
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
80 else:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
81 return self.list_directory(path)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
82 ctype = self.guess_type(path)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
83 try:
47905
0c62a279e4da Issue839496: SimpleHTTPServer should open all files in binary mode.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45550
diff changeset
84 # Always read in binary mode. Opening files in text mode may cause
0c62a279e4da Issue839496: SimpleHTTPServer should open all files in binary mode.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45550
diff changeset
85 # newline translations, making the actual size of the content
0c62a279e4da Issue839496: SimpleHTTPServer should open all files in binary mode.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45550
diff changeset
86 # transmitted *less* than the content-length!
0c62a279e4da Issue839496: SimpleHTTPServer should open all files in binary mode.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 45550
diff changeset
87 f = open(path, 'rb')
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
88 except IOError:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
89 self.send_error(404, "File not found")
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
90 return None
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
91 self.send_response(200)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
92 self.send_header("Content-type", ctype)
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 36279
diff changeset
93 fs = os.fstat(f.fileno())
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 36279
diff changeset
94 self.send_header("Content-Length", str(fs[6]))
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 36279
diff changeset
95 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
96 self.end_headers()
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
97 return f
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
98
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
99 def list_directory(self, path):
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
100 """Helper to produce a directory listing (absent index.html).
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
101
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
102 Return value is either a file object, or None (indicating an
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
103 error). In either case, the headers are sent, making the
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
104 interface the same as for send_head().
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
105
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
106 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
107 try:
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
108 list = os.listdir(path)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
109 except os.error:
16279
785ce6196796 unnecessary semicolon
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
110 self.send_error(404, "No permission to list directory")
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
111 return None
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
112 list.sort(key=lambda a: a.lower())
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
113 f = StringIO()
36047
4db17a00bdcd Patch #1360443: Make SimpleHTTPServer display unencoded directory names.
Georg Brandl <georg@python.org>
parents: 34293
diff changeset
114 displaypath = cgi.escape(urllib.unquote(self.path))
45550
1188ede15735 #2169: make generated HTML more valid
Andrew M. Kuchling <amk@amk.ca>
parents: 45468
diff changeset
115 f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
1188ede15735 #2169: make generated HTML more valid
Andrew M. Kuchling <amk@amk.ca>
parents: 45468
diff changeset
116 f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
1188ede15735 #2169: make generated HTML more valid
Andrew M. Kuchling <amk@amk.ca>
parents: 45468
diff changeset
117 f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
118 f.write("<hr>\n<ul>\n")
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
119 for name in list:
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
120 fullname = os.path.join(path, name)
33214
d9765eb59edd Patch #1011123: Use urllib.quote() instead of cgi.escape() for encoding the
Johannes Gijsbers <jlg@dds.nl>
parents: 33014
diff changeset
121 displayname = linkname = name
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
122 # Append / for directories or @ for symbolic links
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
123 if os.path.isdir(fullname):
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
124 displayname = name + "/"
14400
88853c103087 For this server to work on Windows, directories should use "/" as the
Guido van Rossum <guido@python.org>
parents: 13740
diff changeset
125 linkname = name + "/"
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
126 if os.path.islink(fullname):
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
127 displayname = name + "@"
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
128 # Note: a link to a directory displays with @ and links with /
33214
d9765eb59edd Patch #1011123: Use urllib.quote() instead of cgi.escape() for encoding the
Johannes Gijsbers <jlg@dds.nl>
parents: 33014
diff changeset
129 f.write('<li><a href="%s">%s</a>\n'
d9765eb59edd Patch #1011123: Use urllib.quote() instead of cgi.escape() for encoding the
Johannes Gijsbers <jlg@dds.nl>
parents: 33014
diff changeset
130 % (urllib.quote(linkname), cgi.escape(displayname)))
45550
1188ede15735 #2169: make generated HTML more valid
Andrew M. Kuchling <amk@amk.ca>
parents: 45468
diff changeset
131 f.write("</ul>\n<hr>\n</body>\n</html>\n")
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 16510
diff changeset
132 length = f.tell()
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
133 f.seek(0)
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
134 self.send_response(200)
68640
e9724d7abbc2 Fix issue11442 - Add a charset parameter to the Content-type to avoid XSS attacks.
Senthil Kumaran <orsenthil@gmail.com>
parents: 40513
diff changeset
135 encoding = sys.getfilesystemencoding()
e9724d7abbc2 Fix issue11442 - Add a charset parameter to the Content-type to avoid XSS attacks.
Senthil Kumaran <orsenthil@gmail.com>
parents: 40513
diff changeset
136 self.send_header("Content-type", "text/html; charset=%s" % encoding)
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 16510
diff changeset
137 self.send_header("Content-Length", str(length))
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
138 self.end_headers()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
139 return f
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def translate_path(self, path):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
142 """Translate a /-separated PATH to the local filename syntax.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
144 Components that mean special things to the local file system
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
145 (e.g. drive or directory names) are ignored. (XXX They should
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
146 probably be diagnosed.)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
148 """
36279
5e2a58c867f7 Bug #1394565: SimpleHTTPServer now doesn't choke on query paramters
Georg Brandl <georg@python.org>
parents: 36047
diff changeset
149 # abandon query parameters
45387
0e92ef57c0d4 Issue 1224. Now we support again the double slash in the URL.
Facundo Batista <facundobatista@gmail.com>
parents: 40513
diff changeset
150 path = path.split('?',1)[0]
0e92ef57c0d4 Issue 1224. Now we support again the double slash in the URL.
Facundo Batista <facundobatista@gmail.com>
parents: 40513
diff changeset
151 path = path.split('#',1)[0]
11475
c9cd4bbed8e2 Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents: 10816
diff changeset
152 path = posixpath.normpath(urllib.unquote(path))
16510
c87fdf7de0e6 Correction after translation test.
Eric S. Raymond <esr@thyrsus.com>
parents: 16508
diff changeset
153 words = path.split('/')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
154 words = filter(None, words)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
155 path = os.getcwd()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
156 for word in words:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
157 drive, word = os.path.splitdrive(word)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
158 head, word = os.path.split(word)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
159 if word in (os.curdir, os.pardir): continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
160 path = os.path.join(path, word)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
161 return path
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def copyfile(self, source, outputfile):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
164 """Copy all data between two file objects.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
166 The SOURCE argument is a file object open for reading
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
167 (or anything with a read() method) and the DESTINATION
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
168 argument is a file object open for writing (or
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
169 anything with a write() method).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
171 The only reason for overriding this would be to change
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
172 the block size or perhaps to replace newlines by CRLF
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
173 -- note however that this the default server uses this
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
174 to copy binary data as well.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
176 """
13740
98d8169563e7 Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents: 12724
diff changeset
177 shutil.copyfileobj(source, outputfile)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 def guess_type(self, path):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
180 """Guess the type of a file.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
182 Argument is a PATH (a filename).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
184 Return value is a string of the form type/subtype,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
185 usable for a MIME Content-type header.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
187 The default implementation looks the file's extension
33014
0beb756965df [Bug #866222] Update docstrings.
Andrew M. Kuchling <amk@amk.ca>
parents: 30698
diff changeset
188 up in the table self.extensions_map, using application/octet-stream
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
189 as a default; however it would be permissible (if
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
190 slow) to look inside the data to make a better guess.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
192 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
194 base, ext = posixpath.splitext(path)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22346
diff changeset
195 if ext in self.extensions_map:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
196 return self.extensions_map[ext]
16508
5c3bf2e89d80 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16279
diff changeset
197 ext = ext.lower()
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22346
diff changeset
198 if ext in self.extensions_map:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
199 return self.extensions_map[ext]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
200 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
201 return self.extensions_map['']
38834
32adaad59fb3 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38827
diff changeset
202
38827
d1aec7b4dbb8 Bug #1117556: SimpleHTTPServer now tries to find and use the system's
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
203 if not mimetypes.inited:
d1aec7b4dbb8 Bug #1117556: SimpleHTTPServer now tries to find and use the system's
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
204 mimetypes.init() # try to read system mime.types
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
205 extensions_map = mimetypes.types_map.copy()
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
206 extensions_map.update({
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
207 '': 'application/octet-stream', # Default
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
208 '.py': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
209 '.c': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
210 '.h': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
211 })
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 def test(HandlerClass = SimpleHTTPRequestHandler,
9600
fcbf222e6898 Patch by Jeff Rush:
Guido van Rossum <guido@python.org>
parents: 9598
diff changeset
215 ServerClass = BaseHTTPServer.HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 BaseHTTPServer.test(HandlerClass, ServerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 test()