Mercurial > cpython-lru_cache-weakref
annotate Lib/SimpleHTTPServer.py @ 46567:ffeecbebf3ca
Block 2.6-only doc change.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 26 Apr 2008 18:28:08 +0000 |
| parents | 0cb481956452 |
| children | f762a9ab69dd |
| rev | line source |
|---|---|
| 2887 | 1 """Simple HTTP Server. |
| 2 | |
| 3 This module builds on BaseHTTPServer by implementing the standard GET | |
| 4 and HEAD requests in a fairly straightforward manner. | |
| 5 | |
| 6 """ | |
| 7 | |
| 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 | 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 | 12 |
| 13 import os | |
| 14 import posixpath | |
| 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 |
|
13740
98d8169563e7
Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents:
12724
diff
changeset
|
18 import shutil |
|
15865
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
19 import mimetypes |
|
41760
1a9cd76364e3
Change some uses of cStringIO.StringIO to io.StringIO.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
20 from io import StringIO |
| 2887 | 21 |
| 22 | |
| 23 class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
| 24 | |
| 25 """Simple HTTP request handler with GET and HEAD commands. | |
| 26 | |
| 27 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
|
28 subdirectories. The MIME type for files is determined by |
|
0beb756965df
[Bug #866222] Update docstrings.
Andrew M. Kuchling <amk@amk.ca>
parents:
30698
diff
changeset
|
29 calling the .guess_type() method. |
| 2887 | 30 |
| 31 The GET and HEAD requests are identical except that the HEAD | |
| 32 request omits the actual contents of the file. | |
| 33 | |
| 34 """ | |
| 35 | |
| 36 server_version = "SimpleHTTP/" + __version__ | |
| 37 | |
| 38 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
|
39 """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
|
40 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
|
41 if f: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
42 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
|
43 f.close() |
| 2887 | 44 |
| 45 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
|
46 """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
|
47 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
|
48 if f: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
49 f.close() |
| 2887 | 50 |
| 51 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
|
52 """Common code for GET and HEAD commands. |
| 2887 | 53 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
54 This sends the response code and MIME headers. |
| 2887 | 55 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 None, in which case the caller has nothing further to do. |
| 2887 | 60 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
61 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
62 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
|
63 f = None |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
64 if os.path.isdir(path): |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
65 if not self.path.endswith('/'): |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
66 # redirect browser - doing basically what apache does |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
67 self.send_response(301) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
68 self.send_header("Location", self.path + "/") |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
69 self.end_headers() |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 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
|
74 path = index |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
75 break |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
76 else: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
77 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
|
78 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
|
79 if ctype.startswith('text/'): |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
80 mode = 'r' |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
81 else: |
|
12724
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
82 mode = 'rb' |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
83 try: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
84 f = open(path, mode) |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
85 except IOError: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
86 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
|
87 return None |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
88 self.send_response(200) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
89 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
|
90 fs = os.fstat(f.fileno()) |
|
4668df450672
Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents:
36279
diff
changeset
|
91 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
|
92 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
|
93 self.end_headers() |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
94 return f |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
95 |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
96 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
|
97 """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
|
98 |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
103 """ |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
104 try: |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
105 list = os.listdir(path) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
106 except os.error: |
|
16279
785ce6196796
unnecessary semicolon
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16070
diff
changeset
|
107 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
|
108 return None |
|
30698
3e3fd56ce595
Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
109 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
|
110 f = StringIO() |
|
36047
4db17a00bdcd
Patch #1360443: Make SimpleHTTPServer display unencoded directory names.
Georg Brandl <georg@python.org>
parents:
34293
diff
changeset
|
111 displaypath = cgi.escape(urllib.unquote(self.path)) |
|
45555
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
112 f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
113 f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath) |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
114 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
|
115 f.write("<hr>\n<ul>\n") |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
116 for name in list: |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
117 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
|
118 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
|
119 # 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
|
120 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
|
121 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
|
122 linkname = name + "/" |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
123 if os.path.islink(fullname): |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
124 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
|
125 # 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
|
126 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
|
127 % (urllib.quote(linkname), cgi.escape(displayname))) |
|
45555
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
128 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
|
129 length = f.tell() |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
130 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
|
131 self.send_response(200) |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
132 self.send_header("Content-type", "text/html") |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
16510
diff
changeset
|
133 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
|
134 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
|
135 return f |
| 2887 | 136 |
| 137 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
|
138 """Translate a /-separated PATH to the local filename syntax. |
| 2887 | 139 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
140 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
|
141 (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
|
142 probably be diagnosed.) |
| 2887 | 143 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
144 """ |
|
36279
5e2a58c867f7
Bug #1394565: SimpleHTTPServer now doesn't choke on query paramters
Georg Brandl <georg@python.org>
parents:
36047
diff
changeset
|
145 # abandon query parameters |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
41760
diff
changeset
|
146 path = path.split('?',1)[0] |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
41760
diff
changeset
|
147 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
|
148 path = posixpath.normpath(urllib.unquote(path)) |
|
16510
c87fdf7de0e6
Correction after translation test.
Eric S. Raymond <esr@thyrsus.com>
parents:
16508
diff
changeset
|
149 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
|
150 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
|
151 path = os.getcwd() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 return path |
| 2887 | 158 |
| 159 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
|
160 """Copy all data between two file objects. |
| 2887 | 161 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
162 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
|
163 (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
|
164 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
|
165 anything with a write() method). |
| 2887 | 166 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
167 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
|
168 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
|
169 -- 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
|
170 to copy binary data as well. |
| 2887 | 171 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
172 """ |
|
13740
98d8169563e7
Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents:
12724
diff
changeset
|
173 shutil.copyfileobj(source, outputfile) |
| 2887 | 174 |
| 175 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
|
176 """Guess the type of a file. |
| 2887 | 177 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
178 Argument is a PATH (a filename). |
| 2887 | 179 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
180 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
|
181 usable for a MIME Content-type header. |
| 2887 | 182 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
183 The default implementation looks the file's extension |
|
33014
0beb756965df
[Bug #866222] Update docstrings.
Andrew M. Kuchling <amk@amk.ca>
parents:
30698
diff
changeset
|
184 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
|
185 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
|
186 slow) to look inside the data to make a better guess. |
| 2887 | 187 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
188 """ |
| 2887 | 189 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
190 base, ext = posixpath.splitext(path) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22346
diff
changeset
|
191 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
|
192 return self.extensions_map[ext] |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16279
diff
changeset
|
193 ext = ext.lower() |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22346
diff
changeset
|
194 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
|
195 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
|
196 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
197 return self.extensions_map[''] |
| 2887 | 198 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36555
diff
changeset
|
199 if not mimetypes.inited: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36555
diff
changeset
|
200 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
|
201 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
|
202 extensions_map.update({ |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
203 '': 'application/octet-stream', # Default |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
204 '.py': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
205 '.c': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
206 '.h': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
207 }) |
| 2887 | 208 |
| 209 | |
| 210 def test(HandlerClass = SimpleHTTPRequestHandler, | |
| 9600 | 211 ServerClass = BaseHTTPServer.HTTPServer): |
| 2887 | 212 BaseHTTPServer.test(HandlerClass, ServerClass) |
| 213 | |
| 214 | |
| 215 if __name__ == '__main__': | |
| 216 test() |
