blob: 67371a4bb7c586090186038daed50b5fe7cae51b [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{httplib} ---
Fred Drake12a95691999-04-22 16:47:27 +00002 HTTP protocol client}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake12a95691999-04-22 16:47:27 +00004\declaremodule{standard}{httplib}
Fred Drakec0765c22001-09-25 16:32:02 +00005\modulesynopsis{HTTP and HTTPS protocol client (requires sockets).}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Fred Drakea2e98181998-03-12 05:54:02 +00007\indexii{HTTP}{protocol}
Fred Drakeef338ec2001-12-26 19:48:43 +00008\index{HTTP!\module{httplib} (standard module)}
Guido van Rossuma12ef941995-02-27 17:53:25 +00009
Fred Drakec0765c22001-09-25 16:32:02 +000010This module defines classes which implement the client side of the
11HTTP and HTTPS protocols. It is normally not used directly --- the
12module \refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs
Fred Drake0a9cc582003-01-27 16:32:04 +000013that use HTTP and HTTPS.
14
15\begin{notice}
16 HTTPS support is only available if the \refmodule{socket} module was
17 compiled with SSL support.
18\end{notice}
19
20\begin{notice}
21 The public interface for this module changed substantially in Python
22 2.0. The \class{HTTP} class is retained only for backward
23 compatibility with 1.5.2. It should not be used in new code. Refer
24 to the online docstrings for usage.
25\end{notice}
Guido van Rossuma12ef941995-02-27 17:53:25 +000026
Fred Drake38f3b722001-11-30 06:06:40 +000027The module provides the following classes:
Fred Drake30bd6662001-11-09 05:03:05 +000028
Facundo Batista07c78be2007-03-23 18:54:07 +000029\begin{classdesc}{HTTPConnection}{host\optional{, port\optional{,
30 strict\optional{, timeout}}}}
Fred Drake38f3b722001-11-30 06:06:40 +000031An \class{HTTPConnection} instance represents one transaction with an HTTP
32server. It should be instantiated passing it a host and optional port number.
33If no port number is passed, the port is extracted from the host string if it
34has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is
Facundo Batista07c78be2007-03-23 18:54:07 +000035used.
36When True the optional parameter \var{strict}
37causes \code{BadStatusLine} to be raised if the status line can't be parsed
38as a valid HTTP/1.0 or 1.1 status line. If the optional \var{timeout}
39parameter is given, connection attempts will timeout after that many
40seconds (if no timeout is passed, or is passed as None, the global default
41timeout setting is used).
42
43For example, the following calls all create instances that connect to
Fred Drake38f3b722001-11-30 06:06:40 +000044the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000045
Fred Drake38f3b722001-11-30 06:06:40 +000046\begin{verbatim}
47>>> h1 = httplib.HTTPConnection('www.cwi.nl')
48>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
49>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
Facundo Batista07c78be2007-03-23 18:54:07 +000050>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)
Fred Drake38f3b722001-11-30 06:06:40 +000051\end{verbatim}
Skip Montanaro13a28632003-01-27 15:00:38 +000052\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000053\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000054
Brett Cannon235d1ef2003-05-20 02:56:35 +000055\begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
Fred Drake38f3b722001-11-30 06:06:40 +000056A subclass of \class{HTTPConnection} that uses SSL for communication with
57secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000058\var{key_file} is
59the name of a PEM formatted file that contains your private
60key. \var{cert_file} is a PEM formatted certificate chain file.
61
62\warning{This does not do any certificate verification!}
63
Skip Montanaro13a28632003-01-27 15:00:38 +000064\versionadded{2.0}
65\end{classdesc}
66
67\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
68Class whose instances are returned upon successful connection. Not
69instantiated directly by user.
70\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000071\end{classdesc}
72
73The following exceptions are raised as appropriate:
74
75\begin{excdesc}{HTTPException}
76The base class of the other exceptions in this module. It is a
77subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000078\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000079\end{excdesc}
80
81\begin{excdesc}{NotConnected}
82A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000083\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000084\end{excdesc}
85
Skip Montanaro1e962cb2002-03-24 16:55:57 +000086\begin{excdesc}{InvalidURL}
87A subclass of \exception{HTTPException}, raised if a port is given and is
88either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000089\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000090\end{excdesc}
91
Fred Drake38f3b722001-11-30 06:06:40 +000092\begin{excdesc}{UnknownProtocol}
93A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000094\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000095\end{excdesc}
96
97\begin{excdesc}{UnknownTransferEncoding}
98A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000099\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000100\end{excdesc}
101
102\begin{excdesc}{UnimplementedFileMode}
103A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000104\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000105\end{excdesc}
106
107\begin{excdesc}{IncompleteRead}
108A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000109\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000110\end{excdesc}
111
112\begin{excdesc}{ImproperConnectionState}
113A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000114\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000115\end{excdesc}
116
117\begin{excdesc}{CannotSendRequest}
118A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000119\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000120\end{excdesc}
121
122\begin{excdesc}{CannotSendHeader}
123A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000124\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000125\end{excdesc}
126
127\begin{excdesc}{ResponseNotReady}
128A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000129\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000130\end{excdesc}
131
132\begin{excdesc}{BadStatusLine}
133A subclass of \exception{HTTPException}. Raised if a server responds with a
134HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000135\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000136\end{excdesc}
137
Martin v. Löwis39a31782004-09-18 09:03:49 +0000138The constants defined in this module are:
139
140\begin{datadesc}{HTTP_PORT}
141 The default port for the HTTP protocol (always \code{80}).
142\end{datadesc}
143
144\begin{datadesc}{HTTPS_PORT}
145 The default port for the HTTPS protocol (always \code{443}).
146\end{datadesc}
147
148and also the following constants for integer status codes:
149
150\begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
151 \lineiii{CONTINUE}{\code{100}}
152 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
153 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
154 \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
155 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
156 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
157 \lineiii{PROCESSING}{\code{102}}
158 {WEBDAV, \ulink{RFC 2518, Section 10.1}
Georg Brandl7f26a622005-08-27 17:04:58 +0000159 {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000160
161 \lineiii{OK}{\code{200}}
162 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
163 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
164 \lineiii{CREATED}{\code{201}}
165 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
166 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
167 \lineiii{ACCEPTED}{\code{202}}
168 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
169 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
170 \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
171 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
172 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
173 \lineiii{NO_CONTENT}{\code{204}}
174 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
175 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
176 \lineiii{RESET_CONTENT}{\code{205}}
177 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
178 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
179 \lineiii{PARTIAL_CONTENT}{\code{206}}
180 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
181 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
182 \lineiii{MULTI_STATUS}{\code{207}}
183 {WEBDAV \ulink{RFC 2518, Section 10.2}
Georg Brandl7f26a622005-08-27 17:04:58 +0000184 {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000185 \lineiii{IM_USED}{\code{226}}
186 {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
187
188 \lineiii{MULTIPLE_CHOICES}{\code{300}}
189 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
190 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
191 \lineiii{MOVED_PERMANENTLY}{\code{301}}
192 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
193 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
194 \lineiii{FOUND}{\code{302}}
195 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
196 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
197 \lineiii{SEE_OTHER}{\code{303}}
198 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
199 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
200 \lineiii{NOT_MODIFIED}{\code{304}}
201 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
202 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
203 \lineiii{USE_PROXY}{\code{305}}
204 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
205 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
206 \lineiii{TEMPORARY_REDIRECT}{\code{307}}
207 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
208 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
209
210 \lineiii{BAD_REQUEST}{\code{400}}
211 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
212 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
213 \lineiii{UNAUTHORIZED}{\code{401}}
214 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
215 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
216 \lineiii{PAYMENT_REQUIRED}{\code{402}}
217 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
218 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
219 \lineiii{FORBIDDEN}{\code{403}}
220 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
221 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
222 \lineiii{NOT_FOUND}{\code{404}}
223 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
224 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
225 \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
226 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
227 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
228 \lineiii{NOT_ACCEPTABLE}{\code{406}}
229 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
230 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
231 \lineiii{PROXY_AUTHENTICATION_REQUIRED}
232 {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
233 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
234 \lineiii{REQUEST_TIMEOUT}{\code{408}}
235 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
236 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
237 \lineiii{CONFLICT}{\code{409}}
238 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
239 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
240 \lineiii{GONE}{\code{410}}
241 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
242 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
243 \lineiii{LENGTH_REQUIRED}{\code{411}}
244 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
245 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
246 \lineiii{PRECONDITION_FAILED}{\code{412}}
247 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
248 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
249 \lineiii{REQUEST_ENTITY_TOO_LARGE}
250 {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
251 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
252 \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
253 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
254 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
255 \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
256 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
257 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
258 \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
259 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
260 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
261 \lineiii{EXPECTATION_FAILED}{\code{417}}
262 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
263 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
264 \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
265 {WEBDAV, \ulink{RFC 2518, Section 10.3}
Georg Brandl7f26a622005-08-27 17:04:58 +0000266 {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000267 \lineiii{LOCKED}{\code{423}}
268 {WEBDAV \ulink{RFC 2518, Section 10.4}
Georg Brandl7f26a622005-08-27 17:04:58 +0000269 {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000270 \lineiii{FAILED_DEPENDENCY}{\code{424}}
271 {WEBDAV, \ulink{RFC 2518, Section 10.5}
Georg Brandl7f26a622005-08-27 17:04:58 +0000272 {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000273 \lineiii{UPGRADE_REQUIRED}{\code{426}}
274 {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
275
276 \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
277 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
278 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
279 \lineiii{NOT_IMPLEMENTED}{\code{501}}
280 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
281 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
282 \lineiii{BAD_GATEWAY}{\code{502}}
283 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
284 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
285 \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
286 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
287 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
288 \lineiii{GATEWAY_TIMEOUT}{\code{504}}
289 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
290 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
291 \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
292 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
293 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
294 \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
295 {WEBDAV, \ulink{RFC 2518, Section 10.6}
Georg Brandl7f26a622005-08-27 17:04:58 +0000296 {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000297 \lineiii{NOT_EXTENDED}{\code{510}}
298 {An HTTP Extension Framework, \rfc{2774}, Section 7}
299\end{tableiii}
Fred Drake38f3b722001-11-30 06:06:40 +0000300
Georg Brandl6aab16e2006-02-17 19:17:25 +0000301\begin{datadesc}{responses}
302This dictionary maps the HTTP 1.1 status codes to the W3C names.
303
304Example: \code{httplib.responses[httplib.NOT_FOUND]} is \code{'Not Found'}.
305\versionadded{2.5}
306\end{datadesc}
307
308
Fred Drake38f3b722001-11-30 06:06:40 +0000309\subsection{HTTPConnection Objects \label{httpconnection-objects}}
310
311\class{HTTPConnection} instances have the following methods:
312
313\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
314This will send a request to the server using the HTTP request method
315\var{method} and the selector \var{url}. If the \var{body} argument is
316present, it should be a string of data to send after the headers are finished.
Martin v. Löwis040a9272006-11-12 10:32:47 +0000317Alternatively, it may be an open file object, in which case the
318contents of the file is sent; this file object should support
319\code{fileno()} and \code{read()} methods.
Fred Drake38f3b722001-11-30 06:06:40 +0000320The header Content-Length is automatically set to the correct value.
321The \var{headers} argument should be a mapping of extra HTTP headers to send
322with the request.
Martin v. Löwis040a9272006-11-12 10:32:47 +0000323
324\versionchanged[\var{body} can be a file object]{2.6}
Fred Drake38f3b722001-11-30 06:06:40 +0000325\end{methoddesc}
326
327\begin{methoddesc}{getresponse}{}
328Should be called after a request is sent to get the response from the server.
329Returns an \class{HTTPResponse} instance.
Georg Brandl71de0402005-06-25 19:15:48 +0000330\note{Note that you must have read the whole response before you can send a new
331request to the server.}
Fred Drake38f3b722001-11-30 06:06:40 +0000332\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000333
Fred Drakefc576191998-04-04 07:15:02 +0000334\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000335Set the debugging level (the amount of debugging output printed).
336The default debug level is \code{0}, meaning no debugging output is
337printed.
Fred Drakefc576191998-04-04 07:15:02 +0000338\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000339
Fred Drake38f3b722001-11-30 06:06:40 +0000340\begin{methoddesc}{connect}{}
341Connect to the server specified when the object was created.
342\end{methoddesc}
343
344\begin{methoddesc}{close}{}
345Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000346\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000347
Georg Brandl71de0402005-06-25 19:15:48 +0000348As an alternative to using the \method{request()} method described above,
349you can also send your request step by step, by using the four functions
350below.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000351
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000352\begin{methoddesc}{putrequest}{request, selector\optional{,
353skip\_host\optional{, skip_accept_encoding}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000354This should be the first call after the connection to the server has
355been made. It sends a line to the server consisting of the
356\var{request} string, the \var{selector} string, and the HTTP version
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000357(\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
358\code{Accept-Encoding:} headers (for example to accept additional
359content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
360with non-False values.
361\versionchanged[\var{skip_accept_encoding} argument added]{2.4}
Fred Drakefc576191998-04-04 07:15:02 +0000362\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000363
Fred Drakefc576191998-04-04 07:15:02 +0000364\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000365Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000366server consisting of the header, a colon and a space, and the first
367argument. If more arguments are given, continuation lines are sent,
368each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000369\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000370
Fred Drakefc576191998-04-04 07:15:02 +0000371\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000372Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000373\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000374
Georg Brandl71de0402005-06-25 19:15:48 +0000375\begin{methoddesc}{send}{data}
376Send data to the server. This should be used directly only after the
377\method{endheaders()} method has been called and before
378\method{getresponse()} is called.
379\end{methoddesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000380
381\subsection{HTTPResponse Objects \label{httpresponse-objects}}
382
383\class{HTTPResponse} instances have the following methods and attributes:
384
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000385\begin{methoddesc}{read}{\optional{amt}}
386Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000387\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000388
Fred Drake38f3b722001-11-30 06:06:40 +0000389\begin{methoddesc}{getheader}{name\optional{, default}}
390Get the contents of the header \var{name}, or \var{default} if there is no
391matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000392\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000393
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000394\begin{methoddesc}{getheaders}{}
395Return a list of (header, value) tuples. \versionadded{2.4}
396\end{methoddesc}
397
Fred Drake38f3b722001-11-30 06:06:40 +0000398\begin{datadesc}{msg}
399 A \class{mimetools.Message} instance containing the response headers.
400\end{datadesc}
401
402\begin{datadesc}{version}
403 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
404\end{datadesc}
405
406\begin{datadesc}{status}
407 Status code returned by server.
408\end{datadesc}
409
410\begin{datadesc}{reason}
411 Reason phrase returned by server.
412\end{datadesc}
413
Fred Drakec0765c22001-09-25 16:32:02 +0000414
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000415\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000416
Fred Drake4e716fa2000-06-28 21:51:43 +0000417Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000418
Fred Drake19479911998-02-13 06:58:54 +0000419\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000420>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000421>>> conn = httplib.HTTPConnection("www.python.org")
422>>> conn.request("GET", "/index.html")
423>>> r1 = conn.getresponse()
424>>> print r1.status, r1.reason
425200 OK
426>>> data1 = r1.read()
427>>> conn.request("GET", "/parrot.spam")
428>>> r2 = conn.getresponse()
429>>> print r2.status, r2.reason
430404 Not Found
431>>> data2 = r2.read()
432>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000433\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000434
435Here is an example session that shows how to \samp{POST} requests:
436
437\begin{verbatim}
438>>> import httplib, urllib
439>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000440>>> headers = {"Content-type": "application/x-www-form-urlencoded",
441... "Accept": "text/plain"}
442>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
443>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000444>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000445>>> print response.status, response.reason
446200 OK
447>>> data = response.read()
448>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000449\end{verbatim}