annotate Lib/SimpleHTTPServer.py @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents c87fdf7de0e6
children 27f36f4bf525
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
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
9 __version__ = "0.6"
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15865
diff changeset
11 __all__ = ["SimpleHTTPRequestHandler"]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 import os
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
13740
98d8169563e7 Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents: 12724
diff changeset
18 import shutil
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
19 import mimetypes
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
20 from StringIO import StringIO
2887
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
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 """Simple HTTP request handler with GET and HEAD commands.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 This serves files from the current directory and any of its
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 subdirectories. It assumes that all files are plain text files
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 unless they have the extension ".html" in which case it assumes
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 they are HTML files.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 The GET and HEAD requests are identical except that the HEAD
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 request omits the actual contents of the file.
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 server_version = "SimpleHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 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
40 """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
41 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
42 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
43 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
44 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 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
47 """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
48 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
49 if f:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
50 f.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 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
53 """Common code for GET and HEAD commands.
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 This sends the response code and MIME headers.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
57 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
58 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
59 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
60 None, in which case the caller has nothing further to do.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
62 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
63 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
64 f = None
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
65 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
66 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
67 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
68 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
69 path = index
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
70 break
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
71 else:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
72 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
73 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
74 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
75 mode = 'r'
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
76 else:
12724
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
77 mode = 'rb'
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
78 try:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
79 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
80 except IOError:
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
81 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
82 return None
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
83 self.send_response(200)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
84 self.send_header("Content-type", ctype)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
85 self.end_headers()
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
86 return f
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
87
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
88 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
89 """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
90
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
91 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
92 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
93 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
94
2b498837cd85 Changed list_directory() somewhat. It is now only called when there
Guido van Rossum <guido@python.org>
parents: 12635
diff changeset
95 """
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
96 try:
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
97 list = os.listdir(path)
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
98 except os.error:
16279
785ce6196796 unnecessary semicolon
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
99 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
100 return None
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
101 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
102 f = StringIO()
14400
88853c103087 For this server to work on Windows, directories should use "/" as the
Guido van Rossum <guido@python.org>
parents: 13740
diff changeset
103 f.write("<title>Directory listing for %s</title>\n" % self.path)
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
104 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
105 f.write("<hr>\n<ul>\n")
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
106 for name in list:
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
107 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
108 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
109 # 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
110 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
111 displayname = name + "/"
14400
88853c103087 For this server to work on Windows, directories should use "/" as the
Guido van Rossum <guido@python.org>
parents: 13740
diff changeset
112 linkname = name + "/"
12635
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
113 if os.path.islink(fullname):
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
114 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
115 # 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
116 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
117 f.write("</ul>\n<hr>\n")
bea3d770be0a Add a simple directory listing function.
Guido van Rossum <guido@python.org>
parents: 11475
diff changeset
118 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
119 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
120 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
121 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
122 return f
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 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
125 """Translate a /-separated PATH to the local filename syntax.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
127 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
128 (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
129 probably be diagnosed.)
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 """
11475
c9cd4bbed8e2 Moshe Zadka writes: When deploying SimpleHTTPServer, I noticed a
Guido van Rossum <guido@python.org>
parents: 10816
diff changeset
132 path = posixpath.normpath(urllib.unquote(path))
16510
c87fdf7de0e6 Correction after translation test.
Eric S. Raymond <esr@thyrsus.com>
parents: 16508
diff changeset
133 words = path.split('/')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
134 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
135 path = os.getcwd()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
136 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
137 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
138 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
139 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
140 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
141 return path
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 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
144 """Copy all data between two file objects.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
146 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
147 (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
148 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
149 anything with a write() method).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
151 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
152 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
153 -- 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
154 to copy binary data as well.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
156 """
13740
98d8169563e7 Using shutil's function to copy data between file objects instead
Moshe Zadka <moshez@math.huji.ac.il>
parents: 12724
diff changeset
157 shutil.copyfileobj(source, outputfile)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 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
160 """Guess the type of a file.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
162 Argument is a PATH (a filename).
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 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
165 usable for a MIME Content-type header.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
167 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
168 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
169 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
170 slow) to look inside the data to make a better guess.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
172 """
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 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
175 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
176 return self.extensions_map[ext]
16508
5c3bf2e89d80 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16279
diff changeset
177 ext = ext.lower()
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4199
diff changeset
178 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
179 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
180 else:
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['']
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
15865
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
183 extensions_map = mimetypes.types_map.copy()
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
184 extensions_map.update({
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
185 '': 'application/octet-stream', # Default
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
186 '.py': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
187 '.c': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
188 '.h': 'text/plain',
5578e9b424d4 - Use mimetypes.types_map to initialize extensions_map.
Guido van Rossum <guido@python.org>
parents: 14400
diff changeset
189 })
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def test(HandlerClass = SimpleHTTPRequestHandler,
9600
fcbf222e6898 Patch by Jeff Rush:
Guido van Rossum <guido@python.org>
parents: 9598
diff changeset
193 ServerClass = BaseHTTPServer.HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 BaseHTTPServer.test(HandlerClass, ServerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 test()