annotate Lib/SimpleHTTPServer.py @ 8831:00a6bb65ff29 legacy-trunk

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