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