| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | """Simple HTTP Server.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This module builds on BaseHTTPServer by implementing the standard GET | 
					
						
							|  |  |  | and HEAD requests in a fairly straightforward manner. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:21:25 +00:00
										 |  |  | __version__ = "0.6" | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-20 19:54:20 +00:00
										 |  |  | __all__ = ["SimpleHTTPRequestHandler"] | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import posixpath | 
					
						
							|  |  |  | import BaseHTTPServer | 
					
						
							| 
									
										
										
										
											1999-11-16 19:04:32 +00:00
										 |  |  | import urllib | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  | import cgi | 
					
						
							| 
									
										
										
										
											2000-07-29 05:15:56 +00:00
										 |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2001-01-14 23:21:25 +00:00
										 |  |  | import mimetypes | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  | from StringIO import StringIO | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Simple HTTP request handler with GET and HEAD commands.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This serves files from the current directory and any of its | 
					
						
							| 
									
										
										
										
											2004-08-07 19:02:19 +00:00
										 |  |  |     subdirectories.  The MIME type for files is determined by | 
					
						
							|  |  |  |     calling the .guess_type() method. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     The GET and HEAD requests are identical except that the HEAD | 
					
						
							|  |  |  |     request omits the actual contents of the file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     server_version = "SimpleHTTP/" + __version__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def do_GET(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Serve a GET request.""" | 
					
						
							|  |  |  |         f = self.send_head() | 
					
						
							|  |  |  |         if f: | 
					
						
							|  |  |  |             self.copyfile(f, self.wfile) | 
					
						
							|  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def do_HEAD(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Serve a HEAD request.""" | 
					
						
							|  |  |  |         f = self.send_head() | 
					
						
							|  |  |  |         if f: | 
					
						
							|  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def send_head(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Common code for GET and HEAD commands.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This sends the response code and MIME headers. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Return value is either a file object (which has to be copied | 
					
						
							|  |  |  |         to the outputfile by the caller unless the command was HEAD, | 
					
						
							|  |  |  |         and must be closed by the caller under all circumstances), or | 
					
						
							|  |  |  |         None, in which case the caller has nothing further to do. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         path = self.translate_path(self.path) | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |         f = None | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if os.path.isdir(path): | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |             for index in "index.html", "index.htm": | 
					
						
							|  |  |  |                 index = os.path.join(path, index) | 
					
						
							|  |  |  |                 if os.path.exists(index): | 
					
						
							|  |  |  |                     path = index | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return self.list_directory(path) | 
					
						
							|  |  |  |         ctype = self.guess_type(path) | 
					
						
							|  |  |  |         if ctype.startswith('text/'): | 
					
						
							|  |  |  |             mode = 'r' | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |             mode = 'rb' | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             f = open(path, mode) | 
					
						
							|  |  |  |         except IOError: | 
					
						
							|  |  |  |             self.send_error(404, "File not found") | 
					
						
							|  |  |  |             return None | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.send_response(200) | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         self.send_header("Content-type", ctype) | 
					
						
							| 
									
										
										
										
											2002-03-17 18:37:22 +00:00
										 |  |  |         self.send_header("Content-Length", str(os.fstat(f.fileno())[6])) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.end_headers() | 
					
						
							|  |  |  |         return f | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |     def list_directory(self, path): | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |         """Helper to produce a directory listing (absent index.html).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Return value is either a file object, or None (indicating an | 
					
						
							|  |  |  |         error).  In either case, the headers are sent, making the | 
					
						
							|  |  |  |         interface the same as for send_head(). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             list = os.listdir(path) | 
					
						
							|  |  |  |         except os.error: | 
					
						
							| 
									
										
										
										
											2001-01-26 17:08:32 +00:00
										 |  |  |             self.send_error(404, "No permission to list directory") | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |             return None | 
					
						
							| 
									
										
										
										
											2003-10-16 05:53:16 +00:00
										 |  |  |         list.sort(key=lambda a: a.lower()) | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         f = StringIO() | 
					
						
							| 
									
										
										
										
											2000-09-04 15:55:31 +00:00
										 |  |  |         f.write("<title>Directory listing for %s</title>\n" % self.path) | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         f.write("<h2>Directory listing for %s</h2>\n" % self.path) | 
					
						
							|  |  |  |         f.write("<hr>\n<ul>\n") | 
					
						
							|  |  |  |         for name in list: | 
					
						
							|  |  |  |             fullname = os.path.join(path, name) | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |             displayname = linkname = name = cgi.escape(name) | 
					
						
							|  |  |  |             # Append / for directories or @ for symbolic links | 
					
						
							|  |  |  |             if os.path.isdir(fullname): | 
					
						
							|  |  |  |                 displayname = name + "/" | 
					
						
							| 
									
										
										
										
											2000-09-04 15:55:31 +00:00
										 |  |  |                 linkname = name + "/" | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |             if os.path.islink(fullname): | 
					
						
							|  |  |  |                 displayname = name + "@" | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |                 # Note: a link to a directory displays with @ and links with / | 
					
						
							|  |  |  |             f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname)) | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         f.write("</ul>\n<hr>\n") | 
					
						
							| 
									
										
										
										
											2002-03-17 18:37:22 +00:00
										 |  |  |         length = f.tell() | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         f.seek(0) | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |         self.send_response(200) | 
					
						
							|  |  |  |         self.send_header("Content-type", "text/html") | 
					
						
							| 
									
										
										
										
											2002-03-17 18:37:22 +00:00
										 |  |  |         self.send_header("Content-Length", str(length)) | 
					
						
							| 
									
										
										
										
											2000-05-21 16:25:29 +00:00
										 |  |  |         self.end_headers() | 
					
						
							| 
									
										
										
										
											2000-05-09 14:57:09 +00:00
										 |  |  |         return f | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     def translate_path(self, path): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Translate a /-separated PATH to the local filename syntax.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Components that mean special things to the local file system | 
					
						
							|  |  |  |         (e.g. drive or directory names) are ignored.  (XXX They should | 
					
						
							|  |  |  |         probably be diagnosed.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											1999-11-16 19:04:32 +00:00
										 |  |  |         path = posixpath.normpath(urllib.unquote(path)) | 
					
						
							| 
									
										
										
										
											2001-02-09 10:26:06 +00:00
										 |  |  |         words = path.split('/') | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         words = filter(None, words) | 
					
						
							|  |  |  |         path = os.getcwd() | 
					
						
							|  |  |  |         for word in words: | 
					
						
							|  |  |  |             drive, word = os.path.splitdrive(word) | 
					
						
							|  |  |  |             head, word = os.path.split(word) | 
					
						
							|  |  |  |             if word in (os.curdir, os.pardir): continue | 
					
						
							|  |  |  |             path = os.path.join(path, word) | 
					
						
							|  |  |  |         return path | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def copyfile(self, source, outputfile): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Copy all data between two file objects.
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The SOURCE argument is a file object open for reading | 
					
						
							|  |  |  |         (or anything with a read() method) and the DESTINATION | 
					
						
							|  |  |  |         argument is a file object open for writing (or | 
					
						
							|  |  |  |         anything with a write() method). | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The only reason for overriding this would be to change | 
					
						
							|  |  |  |         the block size or perhaps to replace newlines by CRLF | 
					
						
							|  |  |  |         -- note however that this the default server uses this | 
					
						
							|  |  |  |         to copy binary data as well. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2000-07-29 05:15:56 +00:00
										 |  |  |         shutil.copyfileobj(source, outputfile) | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def guess_type(self, path): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Guess the type of a file.
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Argument is a PATH (a filename). | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Return value is a string of the form type/subtype, | 
					
						
							|  |  |  |         usable for a MIME Content-type header. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The default implementation looks the file's extension | 
					
						
							| 
									
										
										
										
											2004-08-07 19:02:19 +00:00
										 |  |  |         up in the table self.extensions_map, using application/octet-stream | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         as a default; however it would be permissible (if | 
					
						
							|  |  |  |         slow) to look inside the data to make a better guess. | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         base, ext = posixpath.splitext(path) | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |         if ext in self.extensions_map: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             return self.extensions_map[ext] | 
					
						
							| 
									
										
										
										
											2001-02-09 10:18:37 +00:00
										 |  |  |         ext = ext.lower() | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |         if ext in self.extensions_map: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             return self.extensions_map[ext] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return self.extensions_map[''] | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:21:25 +00:00
										 |  |  |     extensions_map = mimetypes.types_map.copy() | 
					
						
							|  |  |  |     extensions_map.update({ | 
					
						
							|  |  |  |         '': 'application/octet-stream', # Default | 
					
						
							|  |  |  |         '.py': 'text/plain', | 
					
						
							|  |  |  |         '.c': 'text/plain', | 
					
						
							|  |  |  |         '.h': 'text/plain', | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(HandlerClass = SimpleHTTPRequestHandler, | 
					
						
							| 
									
										
										
										
											1998-12-07 04:08:30 +00:00
										 |  |  |          ServerClass = BaseHTTPServer.HTTPServer): | 
					
						
							| 
									
										
										
										
											1995-08-04 04:00:20 +00:00
										 |  |  |     BaseHTTPServer.test(HandlerClass, ServerClass) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test() |