Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-CGIHTTPServer.html" /> | |
13 | <link rel="prev" href="module-BaseHTTPServer.html" /> | |
14 | <link rel="parent" href="internet.html" /> | |
15 | <link rel="next" href="module-CGIHTTPServer.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>11.18 SimpleHTTPServer -- Simple HTTP request handler</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="11.17 BaseHTTPServer " | |
25 | href="module-BaseHTTPServer.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="11. Internet Protocols and" | |
28 | href="internet.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="11.19 CGIHTTPServer " | |
31 | href="module-CGIHTTPServer.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-BaseHTTPServer.html">11.17 BaseHTTPServer </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-CGIHTTPServer.html">11.19 CGIHTTPServer </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION00131800000000000000000"> | |
56 | 11.18 <tt class="module">SimpleHTTPServer</tt> -- | |
57 | Simple HTTP request handler</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-SimpleHTTPServer"></A> | |
62 | ||
63 | <P> | |
64 | The <tt class="module">SimpleHTTPServer</tt> module defines a request-handler class, | |
65 | interface-compatible with <tt class="class">BaseHTTPServer.BaseHTTPRequestHandler</tt>, | |
66 | that serves files only from a base directory. | |
67 | ||
68 | <P> | |
69 | The <tt class="module">SimpleHTTPServer</tt> module defines the following class: | |
70 | ||
71 | <P> | |
72 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
73 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3605' xml:id='l2h-3605' class="class">SimpleHTTPRequestHandler</tt></b>(</nobr></td> | |
74 | <td><var>request, client_address, server</var>)</td></tr></table></dt> | |
75 | <dd> | |
76 | This class is used to serve files from the current directory and below, | |
77 | directly mapping the directory structure to HTTP requests. | |
78 | ||
79 | <P> | |
80 | A lot of the work, such as parsing the request, is done by the base | |
81 | class <tt class="class">BaseHTTPServer.BaseHTTPRequestHandler</tt>. This class | |
82 | implements the <tt class="function">do_GET()</tt> and <tt class="function">do_HEAD()</tt> functions. | |
83 | </dl> | |
84 | ||
85 | <P> | |
86 | The <tt class="class">SimpleHTTPRequestHandler</tt> defines the following member | |
87 | variables: | |
88 | ||
89 | <P> | |
90 | <dl><dt><b><tt id='l2h-3606' xml:id='l2h-3606' class="member">server_version</tt></b></dt> | |
91 | <dd> | |
92 | This will be <code>"SimpleHTTP/" + __version__</code>, where <code>__version__</code> | |
93 | is defined in the module. | |
94 | </dl> | |
95 | ||
96 | <P> | |
97 | <dl><dt><b><tt id='l2h-3607' xml:id='l2h-3607' class="member">extensions_map</tt></b></dt> | |
98 | <dd> | |
99 | A dictionary mapping suffixes into MIME types. The default is signified | |
100 | by an empty string, and is considered to be <code>application/octet-stream</code>. | |
101 | The mapping is used case-insensitively, and so should contain only | |
102 | lower-cased keys. | |
103 | </dl> | |
104 | ||
105 | <P> | |
106 | The <tt class="class">SimpleHTTPRequestHandler</tt> defines the following methods: | |
107 | ||
108 | <P> | |
109 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
110 | <td><nobr><b><tt id='l2h-3608' xml:id='l2h-3608' class="method">do_HEAD</tt></b>(</nobr></td> | |
111 | <td><var></var>)</td></tr></table></dt> | |
112 | <dd> | |
113 | This method serves the <code>'HEAD'</code> request type: it sends the | |
114 | headers it would send for the equivalent <code>GET</code> request. See the | |
115 | <tt class="method">do_GET()</tt> method for a more complete explanation of the possible | |
116 | headers. | |
117 | </dl> | |
118 | ||
119 | <P> | |
120 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
121 | <td><nobr><b><tt id='l2h-3609' xml:id='l2h-3609' class="method">do_GET</tt></b>(</nobr></td> | |
122 | <td><var></var>)</td></tr></table></dt> | |
123 | <dd> | |
124 | The request is mapped to a local file by interpreting the request as | |
125 | a path relative to the current working directory. | |
126 | ||
127 | <P> | |
128 | If the request was mapped to a directory, the directory is checked for | |
129 | a file named <code>index.html</code> or <code>index.htm</code> (in that order). | |
130 | If found, the file's contents are returned; otherwise a directory | |
131 | listing is generated by calling the <tt class="method">list_directory()</tt> method. | |
132 | This method uses <tt class="function">os.listdir()</tt> to scan the directory, and | |
133 | returns a <code>404</code> error response if the <tt class="function">listdir()</tt> fails. | |
134 | ||
135 | <P> | |
136 | If the request was mapped to a file, it is opened and the contents are | |
137 | returned. Any <tt class="exception">IOError</tt> exception in opening the requested | |
138 | file is mapped to a <code>404</code>, <code>'File not found'</code> | |
139 | error. Otherwise, the content type is guessed by calling the | |
140 | <tt class="method">guess_type()</tt> method, which in turn uses the | |
141 | <var>extensions_map</var> variable. | |
142 | ||
143 | <P> | |
144 | A <code>'Content-type:'</code> header with the guessed content type is | |
145 | output, followed by a blank line signifying the end of the headers, | |
146 | and then the contents of the file are output. If the file's MIME type | |
147 | starts with <code>text/</code> the file is opened in text mode; otherwise | |
148 | binary mode is used. | |
149 | ||
150 | <P> | |
151 | For example usage, see the implementation of the <tt class="function">test()</tt> | |
152 | function. | |
153 | </dl> | |
154 | ||
155 | <P> | |
156 | <div class="seealso"> | |
157 | <p class="heading">See Also:</p> | |
158 | ||
159 | <dl compact="compact" class="seemodule"> | |
160 | <dt>Module <b><tt class="module"><a href="module-BaseHTTPServer.html">BaseHTTPServer</a></tt>:</b> | |
161 | <dd>Base class implementation for Web server | |
162 | and request handler. | |
163 | </dl> | |
164 | </div> | |
165 | ||
166 | <DIV CLASS="navigation"> | |
167 | <div class='online-navigation'> | |
168 | <p></p><hr /> | |
169 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
170 | <tr> | |
171 | <td class='online-navigation'><a rel="prev" title="11.17 BaseHTTPServer " | |
172 | href="module-BaseHTTPServer.html"><img src='../icons/previous.png' | |
173 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
174 | <td class='online-navigation'><a rel="parent" title="11. Internet Protocols and" | |
175 | href="internet.html"><img src='../icons/up.png' | |
176 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
177 | <td class='online-navigation'><a rel="next" title="11.19 CGIHTTPServer " | |
178 | href="module-CGIHTTPServer.html"><img src='../icons/next.png' | |
179 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
180 | <td align="center" width="100%">Python Library Reference</td> | |
181 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
182 | href="contents.html"><img src='../icons/contents.png' | |
183 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
184 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
185 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
186 | <td class='online-navigation'><a rel="index" title="Index" | |
187 | href="genindex.html"><img src='../icons/index.png' | |
188 | border='0' height='32' alt='Index' width='32' /></A></td> | |
189 | </tr></table> | |
190 | <div class='online-navigation'> | |
191 | <b class="navlabel">Previous:</b> | |
192 | <a class="sectref" rel="prev" href="module-BaseHTTPServer.html">11.17 BaseHTTPServer </A> | |
193 | <b class="navlabel">Up:</b> | |
194 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> | |
195 | <b class="navlabel">Next:</b> | |
196 | <a class="sectref" rel="next" href="module-CGIHTTPServer.html">11.19 CGIHTTPServer </A> | |
197 | </div> | |
198 | </div> | |
199 | <hr /> | |
200 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
201 | </DIV> | |
202 | <!--End of Navigation Panel--> | |
203 | <ADDRESS> | |
204 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
205 | </ADDRESS> | |
206 | </BODY> | |
207 | </HTML> |