| 1 | n/a | """Simple HTTP Server. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This module builds on BaseHTTPServer by implementing the standard GET |
|---|
| 4 | n/a | and HEAD requests in a fairly straightforward manner. |
|---|
| 5 | n/a | |
|---|
| 6 | 1 | """ |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | |
|---|
| 9 | 1 | __version__ = "0.6" |
|---|
| 10 | n/a | |
|---|
| 11 | 1 | __all__ = ["SimpleHTTPRequestHandler"] |
|---|
| 12 | n/a | |
|---|
| 13 | 1 | import os |
|---|
| 14 | 1 | import posixpath |
|---|
| 15 | 1 | import BaseHTTPServer |
|---|
| 16 | 1 | import urllib |
|---|
| 17 | 1 | import cgi |
|---|
| 18 | 1 | import shutil |
|---|
| 19 | 1 | import mimetypes |
|---|
| 20 | 1 | try: |
|---|
| 21 | 1 | from cStringIO import StringIO |
|---|
| 22 | 0 | except ImportError: |
|---|
| 23 | 0 | from StringIO import StringIO |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | 2 | class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | """Simple HTTP request handler with GET and HEAD commands. |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | This serves files from the current directory and any of its |
|---|
| 31 | n/a | subdirectories. The MIME type for files is determined by |
|---|
| 32 | n/a | calling the .guess_type() method. |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | The GET and HEAD requests are identical except that the HEAD |
|---|
| 35 | n/a | request omits the actual contents of the file. |
|---|
| 36 | n/a | |
|---|
| 37 | 1 | """ |
|---|
| 38 | n/a | |
|---|
| 39 | 1 | server_version = "SimpleHTTP/" + __version__ |
|---|
| 40 | n/a | |
|---|
| 41 | 1 | def do_GET(self): |
|---|
| 42 | n/a | """Serve a GET request.""" |
|---|
| 43 | 12 | f = self.send_head() |
|---|
| 44 | 12 | if f: |
|---|
| 45 | 4 | self.copyfile(f, self.wfile) |
|---|
| 46 | 4 | f.close() |
|---|
| 47 | n/a | |
|---|
| 48 | 1 | def do_HEAD(self): |
|---|
| 49 | n/a | """Serve a HEAD request.""" |
|---|
| 50 | 1 | f = self.send_head() |
|---|
| 51 | 1 | if f: |
|---|
| 52 | 1 | f.close() |
|---|
| 53 | n/a | |
|---|
| 54 | 1 | def send_head(self): |
|---|
| 55 | n/a | """Common code for GET and HEAD commands. |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | This sends the response code and MIME headers. |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | Return value is either a file object (which has to be copied |
|---|
| 60 | n/a | to the outputfile by the caller unless the command was HEAD, |
|---|
| 61 | n/a | and must be closed by the caller under all circumstances), or |
|---|
| 62 | n/a | None, in which case the caller has nothing further to do. |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | """ |
|---|
| 65 | 9 | path = self.translate_path(self.path) |
|---|
| 66 | 9 | f = None |
|---|
| 67 | 9 | if os.path.isdir(path): |
|---|
| 68 | 4 | if not self.path.endswith('/'): |
|---|
| 69 | n/a | # redirect browser - doing basically what apache does |
|---|
| 70 | 1 | self.send_response(301) |
|---|
| 71 | 1 | self.send_header("Location", self.path + "/") |
|---|
| 72 | 1 | self.end_headers() |
|---|
| 73 | 1 | return None |
|---|
| 74 | 7 | for index in "index.html", "index.htm": |
|---|
| 75 | 5 | index = os.path.join(path, index) |
|---|
| 76 | 5 | if os.path.exists(index): |
|---|
| 77 | 1 | path = index |
|---|
| 78 | 1 | break |
|---|
| 79 | n/a | else: |
|---|
| 80 | 2 | return self.list_directory(path) |
|---|
| 81 | 6 | ctype = self.guess_type(path) |
|---|
| 82 | 6 | try: |
|---|
| 83 | n/a | # Always read in binary mode. Opening files in text mode may cause |
|---|
| 84 | n/a | # newline translations, making the actual size of the content |
|---|
| 85 | n/a | # transmitted *less* than the content-length! |
|---|
| 86 | 6 | f = open(path, 'rb') |
|---|
| 87 | 2 | except IOError: |
|---|
| 88 | 2 | self.send_error(404, "File not found") |
|---|
| 89 | 2 | return None |
|---|
| 90 | 4 | self.send_response(200) |
|---|
| 91 | 4 | self.send_header("Content-type", ctype) |
|---|
| 92 | 4 | fs = os.fstat(f.fileno()) |
|---|
| 93 | 4 | self.send_header("Content-Length", str(fs[6])) |
|---|
| 94 | 4 | self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) |
|---|
| 95 | 4 | self.end_headers() |
|---|
| 96 | 4 | return f |
|---|
| 97 | n/a | |
|---|
| 98 | 1 | def list_directory(self, path): |
|---|
| 99 | n/a | """Helper to produce a directory listing (absent index.html). |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | Return value is either a file object, or None (indicating an |
|---|
| 102 | n/a | error). In either case, the headers are sent, making the |
|---|
| 103 | n/a | interface the same as for send_head(). |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | """ |
|---|
| 106 | 2 | try: |
|---|
| 107 | 2 | list = os.listdir(path) |
|---|
| 108 | 1 | except os.error: |
|---|
| 109 | 1 | self.send_error(404, "No permission to list directory") |
|---|
| 110 | 1 | return None |
|---|
| 111 | 2 | list.sort(key=lambda a: a.lower()) |
|---|
| 112 | 1 | f = StringIO() |
|---|
| 113 | 1 | displaypath = cgi.escape(urllib.unquote(self.path)) |
|---|
| 114 | 1 | f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') |
|---|
| 115 | 1 | f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath) |
|---|
| 116 | 1 | f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath) |
|---|
| 117 | 1 | f.write("<hr>\n<ul>\n") |
|---|
| 118 | 2 | for name in list: |
|---|
| 119 | 1 | fullname = os.path.join(path, name) |
|---|
| 120 | 1 | displayname = linkname = name |
|---|
| 121 | n/a | # Append / for directories or @ for symbolic links |
|---|
| 122 | 1 | if os.path.isdir(fullname): |
|---|
| 123 | 0 | displayname = name + "/" |
|---|
| 124 | 0 | linkname = name + "/" |
|---|
| 125 | 1 | if os.path.islink(fullname): |
|---|
| 126 | 0 | displayname = name + "@" |
|---|
| 127 | n/a | # Note: a link to a directory displays with @ and links with / |
|---|
| 128 | 1 | f.write('<li><a href="%s">%s</a>\n' |
|---|
| 129 | 1 | % (urllib.quote(linkname), cgi.escape(displayname))) |
|---|
| 130 | 1 | f.write("</ul>\n<hr>\n</body>\n</html>\n") |
|---|
| 131 | 1 | length = f.tell() |
|---|
| 132 | 1 | f.seek(0) |
|---|
| 133 | 1 | self.send_response(200) |
|---|
| 134 | 1 | self.send_header("Content-type", "text/html") |
|---|
| 135 | 1 | self.send_header("Content-Length", str(length)) |
|---|
| 136 | 1 | self.end_headers() |
|---|
| 137 | 1 | return f |
|---|
| 138 | n/a | |
|---|
| 139 | 1 | def translate_path(self, path): |
|---|
| 140 | n/a | """Translate a /-separated PATH to the local filename syntax. |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | Components that mean special things to the local file system |
|---|
| 143 | n/a | (e.g. drive or directory names) are ignored. (XXX They should |
|---|
| 144 | n/a | probably be diagnosed.) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | """ |
|---|
| 147 | n/a | # abandon query parameters |
|---|
| 148 | 22 | path = path.split('?',1)[0] |
|---|
| 149 | 22 | path = path.split('#',1)[0] |
|---|
| 150 | 22 | path = posixpath.normpath(urllib.unquote(path)) |
|---|
| 151 | 22 | words = path.split('/') |
|---|
| 152 | 22 | words = filter(None, words) |
|---|
| 153 | 22 | path = os.getcwd() |
|---|
| 154 | 51 | for word in words: |
|---|
| 155 | 29 | drive, word = os.path.splitdrive(word) |
|---|
| 156 | 29 | head, word = os.path.split(word) |
|---|
| 157 | 29 | if word in (os.curdir, os.pardir): continue |
|---|
| 158 | 25 | path = os.path.join(path, word) |
|---|
| 159 | 22 | return path |
|---|
| 160 | n/a | |
|---|
| 161 | 1 | def copyfile(self, source, outputfile): |
|---|
| 162 | n/a | """Copy all data between two file objects. |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | The SOURCE argument is a file object open for reading |
|---|
| 165 | n/a | (or anything with a read() method) and the DESTINATION |
|---|
| 166 | n/a | argument is a file object open for writing (or |
|---|
| 167 | n/a | anything with a write() method). |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | The only reason for overriding this would be to change |
|---|
| 170 | n/a | the block size or perhaps to replace newlines by CRLF |
|---|
| 171 | n/a | -- note however that this the default server uses this |
|---|
| 172 | n/a | to copy binary data as well. |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | """ |
|---|
| 175 | 4 | shutil.copyfileobj(source, outputfile) |
|---|
| 176 | n/a | |
|---|
| 177 | 1 | def guess_type(self, path): |
|---|
| 178 | n/a | """Guess the type of a file. |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | Argument is a PATH (a filename). |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | Return value is a string of the form type/subtype, |
|---|
| 183 | n/a | usable for a MIME Content-type header. |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | The default implementation looks the file's extension |
|---|
| 186 | n/a | up in the table self.extensions_map, using application/octet-stream |
|---|
| 187 | n/a | as a default; however it would be permissible (if |
|---|
| 188 | n/a | slow) to look inside the data to make a better guess. |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | """ |
|---|
| 191 | n/a | |
|---|
| 192 | 6 | base, ext = posixpath.splitext(path) |
|---|
| 193 | 6 | if ext in self.extensions_map: |
|---|
| 194 | 5 | return self.extensions_map[ext] |
|---|
| 195 | 1 | ext = ext.lower() |
|---|
| 196 | 1 | if ext in self.extensions_map: |
|---|
| 197 | 0 | return self.extensions_map[ext] |
|---|
| 198 | n/a | else: |
|---|
| 199 | 1 | return self.extensions_map[''] |
|---|
| 200 | n/a | |
|---|
| 201 | 1 | if not mimetypes.inited: |
|---|
| 202 | 1 | mimetypes.init() # try to read system mime.types |
|---|
| 203 | 1 | extensions_map = mimetypes.types_map.copy() |
|---|
| 204 | 1 | extensions_map.update({ |
|---|
| 205 | 1 | '': 'application/octet-stream', # Default |
|---|
| 206 | 1 | '.py': 'text/plain', |
|---|
| 207 | 1 | '.c': 'text/plain', |
|---|
| 208 | 1 | '.h': 'text/plain', |
|---|
| 209 | n/a | }) |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | |
|---|
| 212 | 1 | def test(HandlerClass = SimpleHTTPRequestHandler, |
|---|
| 213 | 1 | ServerClass = BaseHTTPServer.HTTPServer): |
|---|
| 214 | 0 | BaseHTTPServer.test(HandlerClass, ServerClass) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | |
|---|
| 217 | 1 | if __name__ == '__main__': |
|---|
| 218 | 0 | test() |
|---|