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 |
Georg Brandl | 45ab233 | 2006-01-13 17:05:56 +0000 | [diff] [blame] | 17 | import urlparse |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 18 | import cgi |
Moshe Zadka | 37c03ff | 2000-07-29 05:15:56 +0000 | [diff] [blame] | 19 | import shutil |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 20 | import mimetypes |
Guido van Rossum | 68937b4 | 2007-05-18 00:51:22 +0000 | [diff] [blame] | 21 | from io import StringIO |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 22 | |
| 23 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 24 | class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 25 | |
| 26 | """Simple HTTP request handler with GET and HEAD commands. |
| 27 | |
| 28 | This serves files from the current directory and any of its |
Andrew M. Kuchling | b839c1f | 2004-08-07 19:02:19 +0000 | [diff] [blame] | 29 | subdirectories. The MIME type for files is determined by |
| 30 | calling the .guess_type() method. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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): |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 66 | if not self.path.endswith('/'): |
| 67 | # redirect browser - doing basically what apache does |
| 68 | self.send_response(301) |
| 69 | self.send_header("Location", self.path + "/") |
| 70 | self.end_headers() |
| 71 | return None |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 72 | for index in "index.html", "index.htm": |
| 73 | index = os.path.join(path, index) |
| 74 | if os.path.exists(index): |
| 75 | path = index |
| 76 | break |
| 77 | else: |
| 78 | return self.list_directory(path) |
| 79 | ctype = self.guess_type(path) |
| 80 | if ctype.startswith('text/'): |
| 81 | mode = 'r' |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 82 | else: |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 83 | mode = 'rb' |
| 84 | try: |
| 85 | f = open(path, mode) |
| 86 | except IOError: |
| 87 | self.send_error(404, "File not found") |
| 88 | return None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | self.send_response(200) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 90 | self.send_header("Content-type", ctype) |
Georg Brandl | 5d07696 | 2006-02-17 13:34:16 +0000 | [diff] [blame] | 91 | fs = os.fstat(f.fileno()) |
| 92 | self.send_header("Content-Length", str(fs[6])) |
| 93 | self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | self.end_headers() |
| 95 | return f |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 97 | def list_directory(self, path): |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 98 | """Helper to produce a directory listing (absent index.html). |
| 99 | |
| 100 | Return value is either a file object, or None (indicating an |
| 101 | error). In either case, the headers are sent, making the |
| 102 | interface the same as for send_head(). |
| 103 | |
| 104 | """ |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 105 | try: |
| 106 | list = os.listdir(path) |
| 107 | except os.error: |
Jeremy Hylton | 5b48c45 | 2001-01-26 17:08:32 +0000 | [diff] [blame] | 108 | self.send_error(404, "No permission to list directory") |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 109 | return None |
Raymond Hettinger | 6b59f5f | 2003-10-16 05:53:16 +0000 | [diff] [blame] | 110 | list.sort(key=lambda a: a.lower()) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 111 | f = StringIO() |
Georg Brandl | 6ee6952 | 2005-12-16 19:36:08 +0000 | [diff] [blame] | 112 | displaypath = cgi.escape(urllib.unquote(self.path)) |
| 113 | f.write("<title>Directory listing for %s</title>\n" % displaypath) |
| 114 | f.write("<h2>Directory listing for %s</h2>\n" % displaypath) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 115 | f.write("<hr>\n<ul>\n") |
| 116 | for name in list: |
| 117 | fullname = os.path.join(path, name) |
Johannes Gijsbers | 6d63a8d | 2004-08-21 10:43:29 +0000 | [diff] [blame] | 118 | displayname = linkname = name |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 119 | # Append / for directories or @ for symbolic links |
| 120 | if os.path.isdir(fullname): |
| 121 | displayname = name + "/" |
Guido van Rossum | 1d105d1 | 2000-09-04 15:55:31 +0000 | [diff] [blame] | 122 | linkname = name + "/" |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 123 | if os.path.islink(fullname): |
| 124 | displayname = name + "@" |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 125 | # Note: a link to a directory displays with @ and links with / |
Johannes Gijsbers | 6d63a8d | 2004-08-21 10:43:29 +0000 | [diff] [blame] | 126 | f.write('<li><a href="%s">%s</a>\n' |
| 127 | % (urllib.quote(linkname), cgi.escape(displayname))) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 128 | f.write("</ul>\n<hr>\n") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 129 | length = f.tell() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 130 | f.seek(0) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 131 | self.send_response(200) |
| 132 | self.send_header("Content-type", "text/html") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 133 | self.send_header("Content-Length", str(length)) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 134 | self.end_headers() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 135 | return f |
| 136 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 137 | def translate_path(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | """Translate a /-separated PATH to the local filename syntax. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 140 | Components that mean special things to the local file system |
| 141 | (e.g. drive or directory names) are ignored. (XXX They should |
| 142 | probably be diagnosed.) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 144 | """ |
Georg Brandl | 45ab233 | 2006-01-13 17:05:56 +0000 | [diff] [blame] | 145 | # abandon query parameters |
| 146 | path = urlparse.urlparse(path)[2] |
Guido van Rossum | d7b147b | 1999-11-16 19:04:32 +0000 | [diff] [blame] | 147 | path = posixpath.normpath(urllib.unquote(path)) |
Eric S. Raymond | 304b6a3 | 2001-02-09 10:26:06 +0000 | [diff] [blame] | 148 | words = path.split('/') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | words = filter(None, words) |
| 150 | path = os.getcwd() |
| 151 | for word in words: |
| 152 | drive, word = os.path.splitdrive(word) |
| 153 | head, word = os.path.split(word) |
| 154 | if word in (os.curdir, os.pardir): continue |
| 155 | path = os.path.join(path, word) |
| 156 | return path |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 157 | |
| 158 | def copyfile(self, source, outputfile): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 159 | """Copy all data between two file objects. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | The SOURCE argument is a file object open for reading |
| 162 | (or anything with a read() method) and the DESTINATION |
| 163 | argument is a file object open for writing (or |
| 164 | anything with a write() method). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 166 | The only reason for overriding this would be to change |
| 167 | the block size or perhaps to replace newlines by CRLF |
| 168 | -- note however that this the default server uses this |
| 169 | to copy binary data as well. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 171 | """ |
Moshe Zadka | 37c03ff | 2000-07-29 05:15:56 +0000 | [diff] [blame] | 172 | shutil.copyfileobj(source, outputfile) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 173 | |
| 174 | def guess_type(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 175 | """Guess the type of a file. |
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 | Argument is a PATH (a filename). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 179 | Return value is a string of the form type/subtype, |
| 180 | usable for a MIME Content-type header. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 182 | The default implementation looks the file's extension |
Andrew M. Kuchling | b839c1f | 2004-08-07 19:02:19 +0000 | [diff] [blame] | 183 | up in the table self.extensions_map, using application/octet-stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 184 | as a default; however it would be permissible (if |
| 185 | slow) to look inside the data to make a better guess. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | base, ext = posixpath.splitext(path) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 190 | if ext in self.extensions_map: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 191 | return self.extensions_map[ext] |
Eric S. Raymond | bf97c9d | 2001-02-09 10:18:37 +0000 | [diff] [blame] | 192 | ext = ext.lower() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 193 | if ext in self.extensions_map: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 194 | return self.extensions_map[ext] |
| 195 | else: |
| 196 | return self.extensions_map[''] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 197 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 198 | if not mimetypes.inited: |
| 199 | mimetypes.init() # try to read system mime.types |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 200 | extensions_map = mimetypes.types_map.copy() |
| 201 | extensions_map.update({ |
| 202 | '': 'application/octet-stream', # Default |
| 203 | '.py': 'text/plain', |
| 204 | '.c': 'text/plain', |
| 205 | '.h': 'text/plain', |
| 206 | }) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | def test(HandlerClass = SimpleHTTPRequestHandler, |
Guido van Rossum | 5c3b384 | 1998-12-07 04:08:30 +0000 | [diff] [blame] | 210 | ServerClass = BaseHTTPServer.HTTPServer): |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 211 | BaseHTTPServer.test(HandlerClass, ServerClass) |
| 212 | |
| 213 | |
| 214 | if __name__ == '__main__': |
| 215 | test() |