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 | 3b7b813 | 1995-09-18 21:50:43 +0000 | [diff] [blame] | 9 | __version__ = "0.3" |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | import os |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 13 | import string |
| 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 | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 17 | |
| 18 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 19 | class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 20 | |
| 21 | """Simple HTTP request handler with GET and HEAD commands. |
| 22 | |
| 23 | This serves files from the current directory and any of its |
| 24 | subdirectories. It assumes that all files are plain text files |
| 25 | unless they have the extension ".html" in which case it assumes |
| 26 | they are HTML files. |
| 27 | |
| 28 | The GET and HEAD requests are identical except that the HEAD |
| 29 | request omits the actual contents of the file. |
| 30 | |
| 31 | """ |
| 32 | |
| 33 | server_version = "SimpleHTTP/" + __version__ |
| 34 | |
| 35 | def do_GET(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | """Serve a GET request.""" |
| 37 | f = self.send_head() |
| 38 | if f: |
| 39 | self.copyfile(f, self.wfile) |
| 40 | f.close() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 41 | |
| 42 | def do_HEAD(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 43 | """Serve a HEAD request.""" |
| 44 | f = self.send_head() |
| 45 | if f: |
| 46 | f.close() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 47 | |
| 48 | def send_head(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 49 | """Common code for GET and HEAD commands. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | This sends the response code and MIME headers. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 53 | Return value is either a file object (which has to be copied |
| 54 | to the outputfile by the caller unless the command was HEAD, |
| 55 | and must be closed by the caller under all circumstances), or |
| 56 | None, in which case the caller has nothing further to do. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 58 | """ |
| 59 | path = self.translate_path(self.path) |
| 60 | if os.path.isdir(path): |
| 61 | self.send_error(403, "Directory listing not supported") |
| 62 | return None |
| 63 | try: |
Guido van Rossum | 391c8b4 | 1998-12-07 03:53:18 +0000 | [diff] [blame] | 64 | f = open(path, 'rb') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 65 | except IOError: |
| 66 | self.send_error(404, "File not found") |
| 67 | return None |
| 68 | self.send_response(200) |
| 69 | self.send_header("Content-type", self.guess_type(path)) |
| 70 | self.end_headers() |
| 71 | return f |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 72 | |
| 73 | def translate_path(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | """Translate a /-separated PATH to the local filename syntax. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 76 | Components that mean special things to the local file system |
| 77 | (e.g. drive or directory names) are ignored. (XXX They should |
| 78 | probably be diagnosed.) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | """ |
Guido van Rossum | d7b147b | 1999-11-16 19:04:32 +0000 | [diff] [blame] | 81 | path = posixpath.normpath(urllib.unquote(path)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 82 | words = string.splitfields(path, '/') |
| 83 | words = filter(None, words) |
| 84 | path = os.getcwd() |
| 85 | for word in words: |
| 86 | drive, word = os.path.splitdrive(word) |
| 87 | head, word = os.path.split(word) |
| 88 | if word in (os.curdir, os.pardir): continue |
| 89 | path = os.path.join(path, word) |
| 90 | return path |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 91 | |
| 92 | def copyfile(self, source, outputfile): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 93 | """Copy all data between two file objects. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | The SOURCE argument is a file object open for reading |
| 96 | (or anything with a read() method) and the DESTINATION |
| 97 | argument is a file object open for writing (or |
| 98 | anything with a write() method). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 100 | The only reason for overriding this would be to change |
| 101 | the block size or perhaps to replace newlines by CRLF |
| 102 | -- note however that this the default server uses this |
| 103 | to copy binary data as well. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | BLOCKSIZE = 8192 |
| 108 | while 1: |
| 109 | data = source.read(BLOCKSIZE) |
| 110 | if not data: break |
| 111 | outputfile.write(data) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 112 | |
| 113 | def guess_type(self, path): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | """Guess the type of a file. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | Argument is a PATH (a filename). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 118 | Return value is a string of the form type/subtype, |
| 119 | usable for a MIME Content-type header. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 121 | The default implementation looks the file's extension |
| 122 | up in the table self.extensions_map, using text/plain |
| 123 | as a default; however it would be permissible (if |
| 124 | slow) to look inside the data to make a better guess. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 126 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 128 | base, ext = posixpath.splitext(path) |
| 129 | if self.extensions_map.has_key(ext): |
| 130 | return self.extensions_map[ext] |
| 131 | ext = string.lower(ext) |
| 132 | if self.extensions_map.has_key(ext): |
| 133 | return self.extensions_map[ext] |
| 134 | else: |
| 135 | return self.extensions_map[''] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 136 | |
| 137 | extensions_map = { |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 138 | '': 'text/plain', # Default, *must* be present |
| 139 | '.html': 'text/html', |
| 140 | '.htm': 'text/html', |
| 141 | '.gif': 'image/gif', |
| 142 | '.jpg': 'image/jpeg', |
| 143 | '.jpeg': 'image/jpeg', |
| 144 | } |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def test(HandlerClass = SimpleHTTPRequestHandler, |
Guido van Rossum | 5c3b384 | 1998-12-07 04:08:30 +0000 | [diff] [blame] | 148 | ServerClass = BaseHTTPServer.HTTPServer): |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 149 | BaseHTTPServer.test(HandlerClass, ServerClass) |
| 150 | |
| 151 | |
| 152 | if __name__ == '__main__': |
| 153 | test() |