blob: ad93534023acd854d29e482547ed08e3a9f7159c [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
Neal Norwitz35cff5f2007-05-22 06:09:24 +000029\begin{classdesc}{HTTPConnection}{host\optional{, port\optional{, strict}}}
Fred Drake38f3b722001-11-30 06:06:40 +000030An \class{HTTPConnection} instance represents one transaction with an HTTP
31server. It should be instantiated passing it a host and optional port number.
32If no port number is passed, the port is extracted from the host string if it
33has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is
Neal Norwitz35cff5f2007-05-22 06:09:24 +000034used. When True, the optional parameter \var{strict}
35causes \code{BadStatusLine} to be raised if the status line can't be parsed
36as a valid HTTP/1.0 or 1.1 status line.
37
38For example, the following calls all create instances that connect to
Fred Drake38f3b722001-11-30 06:06:40 +000039the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000040
Fred Drake38f3b722001-11-30 06:06:40 +000041\begin{verbatim}
42>>> h1 = httplib.HTTPConnection('www.cwi.nl')
43>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
44>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
45\end{verbatim}
Skip Montanaro13a28632003-01-27 15:00:38 +000046\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000047\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000048
Neal Norwitz35cff5f2007-05-22 06:09:24 +000049\begin{classdesc}{HTTPSConnection}{host\optional{, port\optional{,
50 key_file\optional{, cert_file\optional{, strict}}}}}
Fred Drake38f3b722001-11-30 06:06:40 +000051A subclass of \class{HTTPConnection} that uses SSL for communication with
52secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000053\var{key_file} is
54the name of a PEM formatted file that contains your private
55key. \var{cert_file} is a PEM formatted certificate chain file.
56
57\warning{This does not do any certificate verification!}
58
Skip Montanaro13a28632003-01-27 15:00:38 +000059\versionadded{2.0}
60\end{classdesc}
61
62\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
63Class whose instances are returned upon successful connection. Not
64instantiated directly by user.
65\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000066\end{classdesc}
67
68The following exceptions are raised as appropriate:
69
70\begin{excdesc}{HTTPException}
71The base class of the other exceptions in this module. It is a
72subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000073\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000074\end{excdesc}
75
76\begin{excdesc}{NotConnected}
77A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000078\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000079\end{excdesc}
80
Skip Montanaro1e962cb2002-03-24 16:55:57 +000081\begin{excdesc}{InvalidURL}
82A subclass of \exception{HTTPException}, raised if a port is given and is
83either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000084\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000085\end{excdesc}
86
Fred Drake38f3b722001-11-30 06:06:40 +000087\begin{excdesc}{UnknownProtocol}
88A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000089\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000090\end{excdesc}
91
92\begin{excdesc}{UnknownTransferEncoding}
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}{UnimplementedFileMode}
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}{IncompleteRead}
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}{ImproperConnectionState}
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}{CannotSendRequest}
113A subclass of \exception{ImproperConnectionState}.
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}{CannotSendHeader}
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}{ResponseNotReady}
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}{BadStatusLine}
128A subclass of \exception{HTTPException}. Raised if a server responds with a
129HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000130\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000131\end{excdesc}
132
Martin v. Löwis39a31782004-09-18 09:03:49 +0000133The constants defined in this module are:
134
135\begin{datadesc}{HTTP_PORT}
136 The default port for the HTTP protocol (always \code{80}).
137\end{datadesc}
138
139\begin{datadesc}{HTTPS_PORT}
140 The default port for the HTTPS protocol (always \code{443}).
141\end{datadesc}
142
143and also the following constants for integer status codes:
144
145\begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
146 \lineiii{CONTINUE}{\code{100}}
147 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
148 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
149 \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
150 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
151 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
152 \lineiii{PROCESSING}{\code{102}}
153 {WEBDAV, \ulink{RFC 2518, Section 10.1}
Georg Brandl7f26a622005-08-27 17:04:58 +0000154 {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000155
156 \lineiii{OK}{\code{200}}
157 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
158 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
159 \lineiii{CREATED}{\code{201}}
160 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
161 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
162 \lineiii{ACCEPTED}{\code{202}}
163 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
164 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
165 \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
166 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
167 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
168 \lineiii{NO_CONTENT}{\code{204}}
169 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
170 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
171 \lineiii{RESET_CONTENT}{\code{205}}
172 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
173 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
174 \lineiii{PARTIAL_CONTENT}{\code{206}}
175 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
176 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
177 \lineiii{MULTI_STATUS}{\code{207}}
178 {WEBDAV \ulink{RFC 2518, Section 10.2}
Georg Brandl7f26a622005-08-27 17:04:58 +0000179 {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000180 \lineiii{IM_USED}{\code{226}}
181 {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
182
183 \lineiii{MULTIPLE_CHOICES}{\code{300}}
184 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
185 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
186 \lineiii{MOVED_PERMANENTLY}{\code{301}}
187 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
188 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
189 \lineiii{FOUND}{\code{302}}
190 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
191 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
192 \lineiii{SEE_OTHER}{\code{303}}
193 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
194 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
195 \lineiii{NOT_MODIFIED}{\code{304}}
196 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
197 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
198 \lineiii{USE_PROXY}{\code{305}}
199 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
200 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
201 \lineiii{TEMPORARY_REDIRECT}{\code{307}}
202 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
203 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
204
205 \lineiii{BAD_REQUEST}{\code{400}}
206 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
207 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
208 \lineiii{UNAUTHORIZED}{\code{401}}
209 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
210 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
211 \lineiii{PAYMENT_REQUIRED}{\code{402}}
212 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
213 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
214 \lineiii{FORBIDDEN}{\code{403}}
215 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
216 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
217 \lineiii{NOT_FOUND}{\code{404}}
218 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
219 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
220 \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
221 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
222 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
223 \lineiii{NOT_ACCEPTABLE}{\code{406}}
224 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
225 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
226 \lineiii{PROXY_AUTHENTICATION_REQUIRED}
227 {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
228 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
229 \lineiii{REQUEST_TIMEOUT}{\code{408}}
230 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
231 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
232 \lineiii{CONFLICT}{\code{409}}
233 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
234 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
235 \lineiii{GONE}{\code{410}}
236 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
237 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
238 \lineiii{LENGTH_REQUIRED}{\code{411}}
239 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
240 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
241 \lineiii{PRECONDITION_FAILED}{\code{412}}
242 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
243 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
244 \lineiii{REQUEST_ENTITY_TOO_LARGE}
245 {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
246 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
247 \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
248 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
249 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
250 \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
251 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
252 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
253 \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
254 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
255 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
256 \lineiii{EXPECTATION_FAILED}{\code{417}}
257 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
258 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
259 \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
260 {WEBDAV, \ulink{RFC 2518, Section 10.3}
Georg Brandl7f26a622005-08-27 17:04:58 +0000261 {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000262 \lineiii{LOCKED}{\code{423}}
263 {WEBDAV \ulink{RFC 2518, Section 10.4}
Georg Brandl7f26a622005-08-27 17:04:58 +0000264 {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000265 \lineiii{FAILED_DEPENDENCY}{\code{424}}
266 {WEBDAV, \ulink{RFC 2518, Section 10.5}
Georg Brandl7f26a622005-08-27 17:04:58 +0000267 {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000268 \lineiii{UPGRADE_REQUIRED}{\code{426}}
269 {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
270
271 \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
272 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
273 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
274 \lineiii{NOT_IMPLEMENTED}{\code{501}}
275 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
276 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
277 \lineiii{BAD_GATEWAY}{\code{502}}
278 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
279 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
280 \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
281 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
282 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
283 \lineiii{GATEWAY_TIMEOUT}{\code{504}}
284 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
285 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
286 \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
287 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
288 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
289 \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
290 {WEBDAV, \ulink{RFC 2518, Section 10.6}
Georg Brandl7f26a622005-08-27 17:04:58 +0000291 {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000292 \lineiii{NOT_EXTENDED}{\code{510}}
293 {An HTTP Extension Framework, \rfc{2774}, Section 7}
294\end{tableiii}
Fred Drake38f3b722001-11-30 06:06:40 +0000295
Georg Brandl6aab16e2006-02-17 19:17:25 +0000296\begin{datadesc}{responses}
297This dictionary maps the HTTP 1.1 status codes to the W3C names.
298
299Example: \code{httplib.responses[httplib.NOT_FOUND]} is \code{'Not Found'}.
300\versionadded{2.5}
301\end{datadesc}
302
303
Fred Drake38f3b722001-11-30 06:06:40 +0000304\subsection{HTTPConnection Objects \label{httpconnection-objects}}
305
306\class{HTTPConnection} instances have the following methods:
307
308\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
309This will send a request to the server using the HTTP request method
310\var{method} and the selector \var{url}. If the \var{body} argument is
311present, it should be a string of data to send after the headers are finished.
312The header Content-Length is automatically set to the correct value.
313The \var{headers} argument should be a mapping of extra HTTP headers to send
314with the request.
315\end{methoddesc}
316
317\begin{methoddesc}{getresponse}{}
318Should be called after a request is sent to get the response from the server.
319Returns an \class{HTTPResponse} instance.
Georg Brandl71de0402005-06-25 19:15:48 +0000320\note{Note that you must have read the whole response before you can send a new
321request to the server.}
Fred Drake38f3b722001-11-30 06:06:40 +0000322\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000323
Fred Drakefc576191998-04-04 07:15:02 +0000324\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000325Set the debugging level (the amount of debugging output printed).
326The default debug level is \code{0}, meaning no debugging output is
327printed.
Fred Drakefc576191998-04-04 07:15:02 +0000328\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000329
Fred Drake38f3b722001-11-30 06:06:40 +0000330\begin{methoddesc}{connect}{}
331Connect to the server specified when the object was created.
332\end{methoddesc}
333
334\begin{methoddesc}{close}{}
335Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000336\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000337
Georg Brandl71de0402005-06-25 19:15:48 +0000338As an alternative to using the \method{request()} method described above,
339you can also send your request step by step, by using the four functions
340below.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000341
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000342\begin{methoddesc}{putrequest}{request, selector\optional{,
343skip\_host\optional{, skip_accept_encoding}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000344This should be the first call after the connection to the server has
345been made. It sends a line to the server consisting of the
346\var{request} string, the \var{selector} string, and the HTTP version
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000347(\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
348\code{Accept-Encoding:} headers (for example to accept additional
349content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
350with non-False values.
351\versionchanged[\var{skip_accept_encoding} argument added]{2.4}
Fred Drakefc576191998-04-04 07:15:02 +0000352\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000353
Fred Drakefc576191998-04-04 07:15:02 +0000354\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000355Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000356server consisting of the header, a colon and a space, and the first
357argument. If more arguments are given, continuation lines are sent,
358each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000359\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000360
Fred Drakefc576191998-04-04 07:15:02 +0000361\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000362Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000363\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000364
Georg Brandl71de0402005-06-25 19:15:48 +0000365\begin{methoddesc}{send}{data}
366Send data to the server. This should be used directly only after the
367\method{endheaders()} method has been called and before
368\method{getresponse()} is called.
369\end{methoddesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000370
371\subsection{HTTPResponse Objects \label{httpresponse-objects}}
372
373\class{HTTPResponse} instances have the following methods and attributes:
374
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000375\begin{methoddesc}{read}{\optional{amt}}
376Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000377\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000378
Fred Drake38f3b722001-11-30 06:06:40 +0000379\begin{methoddesc}{getheader}{name\optional{, default}}
380Get the contents of the header \var{name}, or \var{default} if there is no
381matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000382\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000383
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000384\begin{methoddesc}{getheaders}{}
385Return a list of (header, value) tuples. \versionadded{2.4}
386\end{methoddesc}
387
Fred Drake38f3b722001-11-30 06:06:40 +0000388\begin{datadesc}{msg}
389 A \class{mimetools.Message} instance containing the response headers.
390\end{datadesc}
391
392\begin{datadesc}{version}
393 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
394\end{datadesc}
395
396\begin{datadesc}{status}
397 Status code returned by server.
398\end{datadesc}
399
400\begin{datadesc}{reason}
401 Reason phrase returned by server.
402\end{datadesc}
403
Fred Drakec0765c22001-09-25 16:32:02 +0000404
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000405\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000406
Fred Drake4e716fa2000-06-28 21:51:43 +0000407Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000408
Fred Drake19479911998-02-13 06:58:54 +0000409\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000410>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000411>>> conn = httplib.HTTPConnection("www.python.org")
412>>> conn.request("GET", "/index.html")
413>>> r1 = conn.getresponse()
414>>> print r1.status, r1.reason
415200 OK
416>>> data1 = r1.read()
417>>> conn.request("GET", "/parrot.spam")
418>>> r2 = conn.getresponse()
419>>> print r2.status, r2.reason
420404 Not Found
421>>> data2 = r2.read()
422>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000423\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000424
425Here is an example session that shows how to \samp{POST} requests:
426
427\begin{verbatim}
428>>> import httplib, urllib
429>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000430>>> headers = {"Content-type": "application/x-www-form-urlencoded",
431... "Accept": "text/plain"}
432>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
433>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000434>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000435>>> print response.status, response.reason
436200 OK
437>>> data = response.read()
438>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000439\end{verbatim}