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