Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 9 | __version__ = "0.6" |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 11 | __all__ = ["SimpleHTTPRequestHandler"] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 12 | |
| 13 | import os |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 14 | import posixpath |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 15 | import BaseHTTPServer |
Guido van Rossum | d7b147b | 1999-11-16 19:04:32 +0000 | [diff] [blame] | 16 | import urllib |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 17 | import cgi |
Moshe Zadka | 37c03ff | 2000-07-29 05:15:56 +0000 | [diff] [blame] | 18 | import shutil |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 19 | import mimetypes |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 20 | from StringIO import StringIO |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 21 | |
| 22 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | """Serve a GET request.""" |
| 41 | f = self.send_head() |
| 42 | if f: |
| 43 | self.copyfile(f, self.wfile) |
| 44 | f.close() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 45 | |
| 46 | def do_HEAD(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 47 | """Serve a HEAD request.""" |
| 48 | f = self.send_head() |
| 49 | if f: |
| 50 | f.close() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 51 | |
| 52 | def send_head(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 53 | """Common code for GET and HEAD commands. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 55 | This sends the response code and MIME headers. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 57 | Return value is either a file object (which has to be copied |
| 58 | to the outputfile by the caller unless the command was HEAD, |
| 59 | and must be closed by the caller under all circumstances), or |
| 60 | None, in which case the caller has nothing further to do. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 62 | """ |
| 63 | path = self.translate_path(self.path) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 64 | f = None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | if os.path.isdir(path): |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 66 | for index in "index.html", "index.htm": |
| 67 | index = os.path.join(path, index) |
| 68 | if os.path.exists(index): |
| 69 | path = index |
| 70 | break |
| 71 | else: |
| 72 | return self.list_directory(path) |
| 73 | ctype = self.guess_type(path) |
| 74 | if ctype.startswith('text/'): |
| 75 | mode = 'r' |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 76 | else: |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 77 | mode = 'rb' |
| 78 | try: |
| 79 | f = open(path, mode) |
| 80 | except IOError: |
| 81 | self.send_error(404, "File not found") |
| 82 | return None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | self.send_response(200) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 84 | self.send_header("Content-type", ctype) |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 85 | self.send_header("Content-Length", str(os.fstat(f.fileno())[6])) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 86 | self.end_headers() |
| 87 | return f |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 89 | def list_directory(self, path): |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 90 | """Helper to produce a directory listing (absent index.html). |
| 91 | |
| 92 | Return value is either a file object, or None (indicating an |
| 93 | error). In either case, the headers are sent, making the |
| 94 | interface the same as for send_head(). |
| 95 | |
| 96 | """ |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 97 | try: |
| 98 | list = os.listdir(path) |
| 99 | except os.error: |
Jeremy Hylton | 5b48c45 | 2001-01-26 17:08:32 +0000 | [diff] [blame] | 100 | self.send_error(404, "No permission to list directory") |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 101 | return None |
| 102 | list.sort(lambda a, b: cmp(a.lower(), b.lower())) |
| 103 | f = StringIO() |
Guido van Rossum | 1d105d1 | 2000-09-04 15:55:31 +0000 | [diff] [blame] | 104 | f.write("<title>Directory listing for %s</title>\n" % self.path) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 105 | f.write("<h2>Directory listing for %s</h2>\n" % self.path) |
| 106 | f.write("<hr>\n<ul>\n") |
| 107 | for name in list: |
| 108 | fullname = os.path.join(path, name) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 109 | displayname = linkname = name = cgi.escape(name) |
| 110 | # Append / for directories or @ for symbolic links |
| 111 | if os.path.isdir(fullname): |
| 112 | displayname = name + "/" |
Guido van Rossum | 1d105d1 | 2000-09-04 15:55:31 +0000 | [diff] [blame] | 113 | linkname = name + "/" |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 114 | if os.path.islink(fullname): |
| 115 | displayname = name + "@" |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 116 | # Note: a link to a directory displays with @ and links with / |
| 117 | f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname)) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 118 | f.write("</ul>\n<hr>\n") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 119 | length = f.tell() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 120 | f.seek(0) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 121 | self.send_response(200) |
| 122 | self.send_header("Content-type", "text/html") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 123 | self.send_header("Content-Length", str(length)) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 124 | self.end_headers() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 125 | return f |
| 126 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 127 | def translate_path(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 128 | """Translate a /-separated PATH to the local filename syntax. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 130 | Components that mean special things to the local file system |
| 131 | (e.g. drive or directory names) are ignored. (XXX They should |
| 132 | probably be diagnosed.) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 134 | """ |
Guido van Rossum | d7b147b | 1999-11-16 19:04:32 +0000 | [diff] [blame] | 135 | path = posixpath.normpath(urllib.unquote(path)) |
Eric S. Raymond | 304b6a3 | 2001-02-09 10:26:06 +0000 | [diff] [blame] | 136 | words = path.split('/') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 137 | words = filter(None, words) |
| 138 | path = os.getcwd() |
| 139 | for word in words: |
| 140 | drive, word = os.path.splitdrive(word) |
| 141 | head, word = os.path.split(word) |
| 142 | if word in (os.curdir, os.pardir): continue |
| 143 | path = os.path.join(path, word) |
| 144 | return path |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 145 | |
| 146 | def copyfile(self, source, outputfile): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 147 | """Copy all data between two file objects. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | The SOURCE argument is a file object open for reading |
| 150 | (or anything with a read() method) and the DESTINATION |
| 151 | argument is a file object open for writing (or |
| 152 | anything with a write() method). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 154 | The only reason for overriding this would be to change |
| 155 | the block size or perhaps to replace newlines by CRLF |
| 156 | -- note however that this the default server uses this |
| 157 | to copy binary data as well. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 159 | """ |
Moshe Zadka | 37c03ff | 2000-07-29 05:15:56 +0000 | [diff] [blame] | 160 | shutil.copyfileobj(source, outputfile) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 161 | |
| 162 | def guess_type(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 163 | """Guess the type of a file. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | Argument is a PATH (a filename). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | Return value is a string of the form type/subtype, |
| 168 | usable for a MIME Content-type header. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | The default implementation looks the file's extension |
| 171 | up in the table self.extensions_map, using text/plain |
| 172 | as a default; however it would be permissible (if |
| 173 | slow) to look inside the data to make a better guess. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 175 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 177 | base, ext = posixpath.splitext(path) |
| 178 | if self.extensions_map.has_key(ext): |
| 179 | return self.extensions_map[ext] |
Eric S. Raymond | bf97c9d | 2001-02-09 10:18:37 +0000 | [diff] [blame] | 180 | ext = ext.lower() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | if self.extensions_map.has_key(ext): |
| 182 | return self.extensions_map[ext] |
| 183 | else: |
| 184 | return self.extensions_map[''] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 186 | extensions_map = mimetypes.types_map.copy() |
| 187 | extensions_map.update({ |
| 188 | '': 'application/octet-stream', # Default |
| 189 | '.py': 'text/plain', |
| 190 | '.c': 'text/plain', |
| 191 | '.h': 'text/plain', |
| 192 | }) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | def test(HandlerClass = SimpleHTTPRequestHandler, |
Guido van Rossum | 5c3b384 | 1998-12-07 04:08:30 +0000 | [diff] [blame] | 196 | ServerClass = BaseHTTPServer.HTTPServer): |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 197 | BaseHTTPServer.test(HandlerClass, ServerClass) |
| 198 | |
| 199 | |
| 200 | if __name__ == '__main__': |
| 201 | test() |