Mercurial > cpython-lru_cache-weakref
annotate Lib/SimpleHTTPServer.py @ 12357:5b59ea41db4e legacy-trunk
Install the docs (with fewer rules).
Add descriptions for the system variables.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 10 Apr 2000 19:36:27 +0000 |
| parents | c9cd4bbed8e2 |
| children | bea3d770be0a |
| 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 | |
|
3105
438f88b55154
recognize a few more file types
Guido van Rossum <guido@python.org>
parents:
2887
diff
changeset
|
9 __version__ = "0.3" |
| 2887 | 10 |
| 11 | |
| 12 import os | |
| 13 import string | |
| 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 |
| 2887 | 17 |
| 18 | |
| 19 class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
| 20 | |
| 21 """Simple HTTP request handler with GET and HEAD commands. | |
| 22 | |
| 23 This serves files from the current directory and any of its | |
| 24 subdirectories. It assumes that all files are plain text files | |
| 25 unless they have the extension ".html" in which case it assumes | |
| 26 they are HTML files. | |
| 27 | |
| 28 The GET and HEAD requests are identical except that the HEAD | |
| 29 request omits the actual contents of the file. | |
| 30 | |
| 31 """ | |
| 32 | |
| 33 server_version = "SimpleHTTP/" + __version__ | |
| 34 | |
| 35 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
|
36 """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
|
37 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
|
38 if f: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
39 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
|
40 f.close() |
| 2887 | 41 |
| 42 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
|
43 """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
|
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 f.close() |
| 2887 | 47 |
| 48 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
|
49 """Common code for GET and HEAD commands. |
| 2887 | 50 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
51 This sends the response code and MIME headers. |
| 2887 | 52 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 None, in which case the caller has nothing further to do. |
| 2887 | 57 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
58 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
59 path = self.translate_path(self.path) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
60 if os.path.isdir(path): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
61 self.send_error(403, "Directory listing not supported") |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
62 return None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
63 try: |
|
9598
54aafe42c0c3
Open the file in binary mode -- so serving images from a Windows box
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
64 f = open(path, 'rb') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
65 except IOError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
66 self.send_error(404, "File not found") |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
67 return None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
68 self.send_response(200) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
69 self.send_header("Content-type", self.guess_type(path)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
70 self.end_headers() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
71 return f |
| 2887 | 72 |
| 73 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
|
74 """Translate a /-separated PATH to the local filename syntax. |
| 2887 | 75 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
76 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
|
77 (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
|
78 probably be diagnosed.) |
| 2887 | 79 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
80 """ |
|
11475
c9cd4bbed8e2
Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents:
10816
diff
changeset
|
81 path = posixpath.normpath(urllib.unquote(path)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
82 words = string.splitfields(path, '/') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
83 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
|
84 path = os.getcwd() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 return path |
| 2887 | 91 |
| 92 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
|
93 """Copy all data between two file objects. |
| 2887 | 94 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
95 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
|
96 (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
|
97 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
|
98 anything with a write() method). |
| 2887 | 99 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
100 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
|
101 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
|
102 -- 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
|
103 to copy binary data as well. |
| 2887 | 104 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
105 """ |
| 2887 | 106 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
107 BLOCKSIZE = 8192 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
108 while 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
109 data = source.read(BLOCKSIZE) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
110 if not data: break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
111 outputfile.write(data) |
| 2887 | 112 |
| 113 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
|
114 """Guess the type of a file. |
| 2887 | 115 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
116 Argument is a PATH (a filename). |
| 2887 | 117 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
118 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
|
119 usable for a MIME Content-type header. |
| 2887 | 120 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 slow) to look inside the data to make a better guess. |
| 2887 | 125 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
126 """ |
| 2887 | 127 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
128 base, ext = posixpath.splitext(path) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
129 if self.extensions_map.has_key(ext): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
130 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
|
131 ext = string.lower(ext) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
132 if self.extensions_map.has_key(ext): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
133 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
|
134 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
135 return self.extensions_map[''] |
| 2887 | 136 |
| 137 extensions_map = { | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
138 '': 'text/plain', # Default, *must* be present |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
139 '.html': 'text/html', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
140 '.htm': 'text/html', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
141 '.gif': 'image/gif', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
142 '.jpg': 'image/jpeg', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
143 '.jpeg': 'image/jpeg', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4199
diff
changeset
|
144 } |
| 2887 | 145 |
| 146 | |
| 147 def test(HandlerClass = SimpleHTTPRequestHandler, | |
| 9600 | 148 ServerClass = BaseHTTPServer.HTTPServer): |
| 2887 | 149 BaseHTTPServer.test(HandlerClass, ServerClass) |
| 150 | |
| 151 | |
| 152 if __name__ == '__main__': | |
| 153 test() |
