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() |
Georg Brandl | 6ee6952 | 2005-12-16 19:36:08 +0000 | [diff] [blame] | 106 | displaypath = cgi.escape(urllib.unquote(self.path)) |
| 107 | f.write("<title>Directory listing for %s</title>\n" % displaypath) |
| 108 | f.write("<h2>Directory listing for %s</h2>\n" % displaypath) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 109 | f.write("<hr>\n<ul>\n") |
| 110 | for name in list: |
| 111 | fullname = os.path.join(path, name) |
Johannes Gijsbers | 6d63a8d | 2004-08-21 10:43:29 +0000 | [diff] [blame] | 112 | displayname = linkname = name |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 113 | # Append / for directories or @ for symbolic links |
| 114 | if os.path.isdir(fullname): |
| 115 | displayname = name + "/" |
Guido van Rossum | 1d105d1 | 2000-09-04 15:55:31 +0000 | [diff] [blame] | 116 | linkname = name + "/" |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 117 | if os.path.islink(fullname): |
| 118 | displayname = name + "@" |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 119 | # Note: a link to a directory displays with @ and links with / |
Johannes Gijsbers | 6d63a8d | 2004-08-21 10:43:29 +0000 | [diff] [blame] | 120 | f.write('<li><a href="%s">%s</a>\n' |
| 121 | % (urllib.quote(linkname), cgi.escape(displayname))) |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 122 | f.write("</ul>\n<hr>\n") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 123 | length = f.tell() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 124 | f.seek(0) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 125 | self.send_response(200) |
| 126 | self.send_header("Content-type", "text/html") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 127 | self.send_header("Content-Length", str(length)) |
Guido van Rossum | 1d10f3e | 2000-05-21 16:25:29 +0000 | [diff] [blame] | 128 | self.end_headers() |
Guido van Rossum | 57af072 | 2000-05-09 14:57:09 +0000 | [diff] [blame] | 129 | return f |
| 130 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 131 | def translate_path(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 132 | """Translate a /-separated PATH to the local filename syntax. |
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 | Components that mean special things to the local file system |
| 135 | (e.g. drive or directory names) are ignored. (XXX They should |
| 136 | probably be diagnosed.) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | """ |
Guido van Rossum | d7b147b | 1999-11-16 19:04:32 +0000 | [diff] [blame] | 139 | path = posixpath.normpath(urllib.unquote(path)) |
Eric S. Raymond | 304b6a3 | 2001-02-09 10:26:06 +0000 | [diff] [blame] | 140 | words = path.split('/') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 141 | words = filter(None, words) |
| 142 | path = os.getcwd() |
| 143 | for word in words: |
| 144 | drive, word = os.path.splitdrive(word) |
| 145 | head, word = os.path.split(word) |
| 146 | if word in (os.curdir, os.pardir): continue |
| 147 | path = os.path.join(path, word) |
| 148 | return path |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 149 | |
| 150 | def copyfile(self, source, outputfile): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | """Copy all data between two file objects. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | The SOURCE argument is a file object open for reading |
| 154 | (or anything with a read() method) and the DESTINATION |
| 155 | argument is a file object open for writing (or |
| 156 | anything with a write() method). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 158 | The only reason for overriding this would be to change |
| 159 | the block size or perhaps to replace newlines by CRLF |
| 160 | -- note however that this the default server uses this |
| 161 | to copy binary data as well. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 163 | """ |
Moshe Zadka | 37c03ff | 2000-07-29 05:15:56 +0000 | [diff] [blame] | 164 | shutil.copyfileobj(source, outputfile) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 165 | |
| 166 | def guess_type(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | """Guess the type of a file. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | Argument is a PATH (a filename). |
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 | Return value is a string of the form type/subtype, |
| 172 | usable for a MIME Content-type header. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 174 | The default implementation looks the file's extension |
Andrew M. Kuchling | b839c1f | 2004-08-07 19:02:19 +0000 | [diff] [blame] | 175 | up in the table self.extensions_map, using application/octet-stream |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 176 | as a default; however it would be permissible (if |
| 177 | slow) to look inside the data to make a better guess. |
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 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | base, ext = posixpath.splitext(path) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 182 | if ext in self.extensions_map: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 183 | return self.extensions_map[ext] |
Eric S. Raymond | bf97c9d | 2001-02-09 10:18:37 +0000 | [diff] [blame] | 184 | ext = ext.lower() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 185 | if ext in self.extensions_map: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 186 | return self.extensions_map[ext] |
| 187 | else: |
| 188 | return self.extensions_map[''] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 077153e | 2001-01-14 23:21:25 +0000 | [diff] [blame] | 190 | extensions_map = mimetypes.types_map.copy() |
| 191 | extensions_map.update({ |
| 192 | '': 'application/octet-stream', # Default |
| 193 | '.py': 'text/plain', |
| 194 | '.c': 'text/plain', |
| 195 | '.h': 'text/plain', |
| 196 | }) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | def test(HandlerClass = SimpleHTTPRequestHandler, |
Guido van Rossum | 5c3b384 | 1998-12-07 04:08:30 +0000 | [diff] [blame] | 200 | ServerClass = BaseHTTPServer.HTTPServer): |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 201 | BaseHTTPServer.test(HandlerClass, ServerClass) |
| 202 | |
| 203 | |
| 204 | if __name__ == '__main__': |
| 205 | test() |