Mercurial > cpython-lru_cache-weakref
annotate Lib/SimpleHTTPServer.py @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | a1de0bab5475 |
| children | 3e3fd56ce595 |
| 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 |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
20 from StringIO 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 | |
| 28 subdirectories. It assumes that all files are plain text files | |
| 29 unless they have the extension ".html" in which case it assumes | |
| 30 they are HTML files. | |
| 31 | |
| 32 The GET and HEAD requests are identical except that the HEAD | |
| 33 request omits the actual contents of the file. | |
| 34 | |
| 35 """ | |
| 36 | |
| 37 server_version = "SimpleHTTP/" + __version__ | |
| 38 | |
| 39 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
|
40 """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
|
41 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
|
42 if f: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
43 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
|
44 f.close() |
| 2887 | 45 |
| 46 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
|
47 """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
|
48 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
|
49 if f: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
50 f.close() |
| 2887 | 51 |
| 52 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
|
53 """Common code for GET and HEAD commands. |
| 2887 | 54 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
55 This sends the response code and MIME headers. |
| 2887 | 56 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 None, in which case the caller has nothing further to do. |
| 2887 | 61 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
62 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
63 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
|
64 f = None |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
65 if os.path.isdir(path): |
|
12724
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 path = index |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
70 break |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
71 else: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 mode = 'r' |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
76 else: |
|
12724
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
77 mode = 'rb' |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
78 try: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
79 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
|
80 except IOError: |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
81 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
|
82 return None |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
83 self.send_response(200) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
84 self.send_header("Content-type", ctype) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
16510
diff
changeset
|
85 self.send_header("Content-Length", str(os.fstat(f.fileno())[6])) |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
86 self.end_headers() |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
87 return f |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
88 |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
89 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
|
90 """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
|
91 |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
96 """ |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
97 try: |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
98 list = os.listdir(path) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
99 except os.error: |
|
16279
785ce6196796
unnecessary semicolon
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16070
diff
changeset
|
100 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
|
101 return None |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
102 list.sort(lambda a, b: cmp(a.lower(), b.lower())) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
103 f = StringIO() |
|
14400
88853c103087
For this server to work on Windows, directories should use "/" as the
Guido van Rossum <guido@python.org>
parents:
13740
diff
changeset
|
104 f.write("<title>Directory listing for %s</title>\n" % self.path) |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
105 f.write("<h2>Directory listing for %s</h2>\n" % self.path) |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
106 f.write("<hr>\n<ul>\n") |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
107 for name in list: |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
108 fullname = os.path.join(path, name) |
|
12724
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
109 displayname = linkname = name = cgi.escape(name) |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
110 # 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
|
111 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
|
112 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
|
113 linkname = name + "/" |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
114 if os.path.islink(fullname): |
|
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
115 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
|
116 # Note: a link to a directory displays with @ and links with / |
|
2b498837cd85
Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents:
12635
diff
changeset
|
117 f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname)) |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
118 f.write("</ul>\n<hr>\n") |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
16510
diff
changeset
|
119 length = f.tell() |
|
12635
bea3d770be0a
Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents:
11475
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 return f |
| 2887 | 126 |
| 127 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
|
128 """Translate a /-separated PATH to the local filename syntax. |
| 2887 | 129 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
130 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
|
131 (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
|
132 probably be diagnosed.) |
| 2887 | 133 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
134 """ |
|
11475
c9cd4bbed8e2
Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents:
10816
diff
changeset
|
135 path = posixpath.normpath(urllib.unquote(path)) |
|
16510
c87fdf7de0e6
Correction after translation test.
Eric S. Raymond <esr@thyrsus.com>
parents:
16508
diff
changeset
|
136 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
|
137 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
|
138 path = os.getcwd() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 return path |
| 2887 | 145 |
| 146 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
|
147 """Copy all data between two file objects. |
| 2887 | 148 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
149 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
|
150 (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
|
151 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
|
152 anything with a write() method). |
| 2887 | 153 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
154 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
|
155 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
|
156 -- 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
|
157 to copy binary data as well. |
| 2887 | 158 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
159 """ |
|
13740
98d8169563e7
Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents:
12724
diff
changeset
|
160 shutil.copyfileobj(source, outputfile) |
| 2887 | 161 |
| 162 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
|
163 """Guess the type of a file. |
| 2887 | 164 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
165 Argument is a PATH (a filename). |
| 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 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
|
168 usable for a MIME Content-type header. |
| 2887 | 169 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
170 The default implementation looks the file's extension |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
171 up in the table self.extensions_map, using text/plain |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
172 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
|
173 slow) to look inside the data to make a better guess. |
| 2887 | 174 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
175 """ |
| 2887 | 176 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
177 base, ext = posixpath.splitext(path) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22346
diff
changeset
|
178 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
|
179 return self.extensions_map[ext] |
|
16508
5c3bf2e89d80
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16279
diff
changeset
|
180 ext = ext.lower() |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22346
diff
changeset
|
181 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
|
182 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
|
183 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
184 return self.extensions_map[''] |
| 2887 | 185 |
|
15865
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
186 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
|
187 extensions_map.update({ |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
188 '': 'application/octet-stream', # Default |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
189 '.py': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
190 '.c': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
191 '.h': 'text/plain', |
|
5578e9b424d4
- Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents:
14400
diff
changeset
|
192 }) |
| 2887 | 193 |
| 194 | |
| 195 def test(HandlerClass = SimpleHTTPRequestHandler, | |
| 9600 | 196 ServerClass = BaseHTTPServer.HTTPServer): |
| 2887 | 197 BaseHTTPServer.test(HandlerClass, ServerClass) |
| 198 | |
| 199 | |
| 200 if __name__ == '__main__': | |
| 201 test() |
