annotate Lib/SimpleHTTPServer.py @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents 2b498837cd85
children 98d8169563e7
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
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
9 __version__ = "0.4"
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 string
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import posixpath
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import BaseHTTPServer
11475
c9cd4bbed8e2 Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents: 10816
diff changeset
16 import urllib
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
17 import cgi
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
18 from StringIO import StringIO
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
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 class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 """Simple HTTP request handler with GET and HEAD commands.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 This serves files from the current directory and any of its
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 subdirectories. It assumes that all files are plain text files
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 unless they have the extension ".html" in which case it assumes
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 they are HTML files.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 The GET and HEAD requests are identical except that the HEAD
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 request omits the actual contents of the file.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
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 server_version = "SimpleHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 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
38 """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
39 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
40 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
41 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
42 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 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
45 """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
46 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
47 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
48 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 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
51 """Common code for GET and HEAD commands.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
53 This sends the response code and MIME headers.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
55 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
56 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
57 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
58 None, in which case the caller has nothing further to do.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
60 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
61 path = self.translate_path(self.path)
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
62 f = None
7755
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):
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
64 for index in "index.html", "index.htm":
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
65 index = os.path.join(path, index)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
66 if os.path.exists(index):
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
67 path = index
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
68 break
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
69 else:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
70 return self.list_directory(path)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
71 ctype = self.guess_type(path)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
72 if ctype.startswith('text/'):
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
73 mode = 'r'
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
74 else:
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
75 mode = 'rb'
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
76 try:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
77 f = open(path, mode)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
78 except IOError:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
79 self.send_error(404, "File not found")
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
80 return None
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
81 self.send_response(200)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
82 self.send_header("Content-type", ctype)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
83 self.end_headers()
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
84 return f
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
85
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
86 def list_directory(self, path):
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
87 """Helper to produce a directory listing (absent index.html).
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
88
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
89 Return value is either a file object, or None (indicating an
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
90 error). In either case, the headers are sent, making the
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
91 interface the same as for send_head().
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
92
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
93 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
94 try:
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
95 list = os.listdir(path)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
96 except os.error:
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
97 self.send_error(404, "No permission to list directory");
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
98 return None
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
99 list.sort(lambda a, b: cmp(a.lower(), b.lower()))
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
100 f = StringIO()
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
101 f.write("<h2>Directory listing for %s</h2>\n" % self.path)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
102 f.write("<hr>\n<ul>\n")
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
103 for name in list:
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
104 fullname = os.path.join(path, name)
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
105 displayname = linkname = name = cgi.escape(name)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
106 # Append / for directories or @ for symbolic links
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
107 if os.path.isdir(fullname):
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
108 displayname = name + "/"
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
109 linkname = name + os.sep
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
110 if os.path.islink(fullname):
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
111 displayname = name + "@"
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
112 # Note: a link to a directory displays with @ and links with /
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
113 f.write('<li><a href="%s">%s</a>\n' % (linkname, displayname))
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
114 f.write("</ul>\n<hr>\n")
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
115 f.seek(0)
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
116 self.send_response(200)
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
117 self.send_header("Content-type", "text/html")
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
118 self.end_headers()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
119 return f
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 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
122 """Translate a /-separated PATH to the local filename syntax.
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 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
125 (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
126 probably be diagnosed.)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
128 """
11475
c9cd4bbed8e2 Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents: 10816
diff changeset
129 path = posixpath.normpath(urllib.unquote(path))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
130 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
131 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
132 path = os.getcwd()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
133 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
134 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
135 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
136 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
137 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
138 return path
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 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
141 """Copy all data between two file objects.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
143 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
144 (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
145 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
146 anything with a write() method).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
148 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
149 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
150 -- 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
151 to copy binary data as well.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
153 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
155 BLOCKSIZE = 8192
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
156 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
157 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
158 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
159 outputfile.write(data)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 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
162 """Guess the type of a file.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
164 Argument is a PATH (a filename).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
166 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
167 usable for a MIME Content-type header.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
169 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
170 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
171 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
172 slow) to look inside the data to make a better guess.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
174 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
176 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
177 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
178 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
179 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
180 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
181 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
182 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
183 return self.extensions_map['']
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 extensions_map = {
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
186 '': '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
187 '.html': 'text/html',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
188 '.htm': 'text/html',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
189 '.gif': 'image/gif',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
190 '.jpg': 'image/jpeg',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
191 '.jpeg': 'image/jpeg',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
192 }
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 def test(HandlerClass = SimpleHTTPRequestHandler,
9600
fcbf222e6898 Patch by Jeff Rush:
Guido van Rossum <guido@python.org>
parents: 9598
diff changeset
196 ServerClass = BaseHTTPServer.HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 BaseHTTPServer.test(HandlerClass, ServerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 test()