1996-07-22 23:23:25 +08:00
|
|
|
"""Simple HTTP Server.
|
|
|
|
|
|
|
|
This module builds on BaseHTTPServer by implementing the standard GET
|
|
|
|
and HEAD requests in a fairly straightforward manner.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
__version__ = "0.3"
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import string
|
|
|
|
import posixpath
|
|
|
|
import BaseHTTPServer
|
2000-05-09 01:31:04 +08:00
|
|
|
import urllib
|
1996-07-22 23:23:25 +08: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
|
|
|
|
subdirectories. It assumes that all files are plain text files
|
|
|
|
unless they have the extension ".html" in which case it assumes
|
|
|
|
they are HTML files.
|
|
|
|
|
|
|
|
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-27 06:14:20 +08:00
|
|
|
"""Serve a GET request."""
|
|
|
|
f = self.send_head()
|
|
|
|
if f:
|
|
|
|
self.copyfile(f, self.wfile)
|
|
|
|
f.close()
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
def do_HEAD(self):
|
1998-03-27 06:14:20 +08:00
|
|
|
"""Serve a HEAD request."""
|
|
|
|
f = self.send_head()
|
|
|
|
if f:
|
|
|
|
f.close()
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
def send_head(self):
|
1998-03-27 06:14:20 +08: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)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
self.send_error(403, "Directory listing not supported")
|
|
|
|
return None
|
|
|
|
try:
|
1998-12-22 21:50:33 +08:00
|
|
|
f = open(path, 'rb')
|
1998-03-27 06:14:20 +08:00
|
|
|
except IOError:
|
|
|
|
self.send_error(404, "File not found")
|
|
|
|
return None
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", self.guess_type(path))
|
|
|
|
self.end_headers()
|
|
|
|
return f
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
def translate_path(self, path):
|
1998-03-27 06:14:20 +08: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.)
|
|
|
|
|
|
|
|
"""
|
2000-05-09 01:31:04 +08:00
|
|
|
path = posixpath.normpath(urllib.unquote(path))
|
1998-03-27 06:14:20 +08:00
|
|
|
words = string.splitfields(path, '/')
|
|
|
|
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
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
def copyfile(self, source, outputfile):
|
1998-03-27 06:14:20 +08:00
|
|
|
"""Copy all data between two file objects.
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08: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).
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08: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.
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
"""
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
BLOCKSIZE = 8192
|
|
|
|
while 1:
|
|
|
|
data = source.read(BLOCKSIZE)
|
|
|
|
if not data: break
|
|
|
|
outputfile.write(data)
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
def guess_type(self, path):
|
1998-03-27 06:14:20 +08:00
|
|
|
"""Guess the type of a file.
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
Argument is a PATH (a filename).
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
Return value is a string of the form type/subtype,
|
|
|
|
usable for a MIME Content-type header.
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
The default implementation looks the file's extension
|
|
|
|
up in the table self.extensions_map, using text/plain
|
|
|
|
as a default; however it would be permissible (if
|
|
|
|
slow) to look inside the data to make a better guess.
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
"""
|
1996-07-22 23:23:25 +08:00
|
|
|
|
1998-03-27 06:14:20 +08:00
|
|
|
base, ext = posixpath.splitext(path)
|
|
|
|
if self.extensions_map.has_key(ext):
|
|
|
|
return self.extensions_map[ext]
|
|
|
|
ext = string.lower(ext)
|
|
|
|
if self.extensions_map.has_key(ext):
|
|
|
|
return self.extensions_map[ext]
|
|
|
|
else:
|
|
|
|
return self.extensions_map['']
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
extensions_map = {
|
1998-03-27 06:14:20 +08:00
|
|
|
'': 'text/plain', # Default, *must* be present
|
|
|
|
'.html': 'text/html',
|
|
|
|
'.htm': 'text/html',
|
|
|
|
'.gif': 'image/gif',
|
|
|
|
'.jpg': 'image/jpeg',
|
|
|
|
'.jpeg': 'image/jpeg',
|
|
|
|
}
|
1996-07-22 23:23:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test(HandlerClass = SimpleHTTPRequestHandler,
|
1998-12-22 21:50:33 +08:00
|
|
|
ServerClass = BaseHTTPServer.HTTPServer):
|
1996-07-22 23:23:25 +08:00
|
|
|
BaseHTTPServer.test(HandlerClass, ServerClass)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|