blob: 8f02368e2270d85355497b083d93f7ee9ed537e8 [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
Fred Drake38f3b722001-11-30 06:06:40 +000029\begin{classdesc}{HTTPConnection}{host\optional{, port}}
30An \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
34used. For example, the following calls all create instances that connect to
35the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000036
Fred Drake38f3b722001-11-30 06:06:40 +000037\begin{verbatim}
38>>> h1 = httplib.HTTPConnection('www.cwi.nl')
39>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
40>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
41\end{verbatim}
Skip Montanaro13a28632003-01-27 15:00:38 +000042\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000043\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000044
Brett Cannon235d1ef2003-05-20 02:56:35 +000045\begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
Fred Drake38f3b722001-11-30 06:06:40 +000046A subclass of \class{HTTPConnection} that uses SSL for communication with
47secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000048\var{key_file} is
49the name of a PEM formatted file that contains your private
50key. \var{cert_file} is a PEM formatted certificate chain file.
51
52\warning{This does not do any certificate verification!}
53
Skip Montanaro13a28632003-01-27 15:00:38 +000054\versionadded{2.0}
55\end{classdesc}
56
57\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
58Class whose instances are returned upon successful connection. Not
59instantiated directly by user.
60\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000061\end{classdesc}
62
63The following exceptions are raised as appropriate:
64
65\begin{excdesc}{HTTPException}
66The base class of the other exceptions in this module. It is a
67subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000068\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000069\end{excdesc}
70
71\begin{excdesc}{NotConnected}
72A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000073\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000074\end{excdesc}
75
Skip Montanaro1e962cb2002-03-24 16:55:57 +000076\begin{excdesc}{InvalidURL}
77A subclass of \exception{HTTPException}, raised if a port is given and is
78either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000079\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000080\end{excdesc}
81
Fred Drake38f3b722001-11-30 06:06:40 +000082\begin{excdesc}{UnknownProtocol}
83A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000084\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000085\end{excdesc}
86
87\begin{excdesc}{UnknownTransferEncoding}
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}{UnimplementedFileMode}
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}{IncompleteRead}
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}{ImproperConnectionState}
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}{CannotSendRequest}
108A subclass of \exception{ImproperConnectionState}.
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}{CannotSendHeader}
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}{ResponseNotReady}
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}{BadStatusLine}
123A subclass of \exception{HTTPException}. Raised if a server responds with a
124HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000125\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000126\end{excdesc}
127
Martin v. Löwis39a31782004-09-18 09:03:49 +0000128The constants defined in this module are:
129
130\begin{datadesc}{HTTP_PORT}
131 The default port for the HTTP protocol (always \code{80}).
132\end{datadesc}
133
134\begin{datadesc}{HTTPS_PORT}
135 The default port for the HTTPS protocol (always \code{443}).
136\end{datadesc}
137
138and also the following constants for integer status codes:
139
140\begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
141 \lineiii{CONTINUE}{\code{100}}
142 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
143 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
144 \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
145 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
146 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
147 \lineiii{PROCESSING}{\code{102}}
148 {WEBDAV, \ulink{RFC 2518, Section 10.1}
Georg Brandl7f26a622005-08-27 17:04:58 +0000149 {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000150
151 \lineiii{OK}{\code{200}}
152 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
153 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
154 \lineiii{CREATED}{\code{201}}
155 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
156 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
157 \lineiii{ACCEPTED}{\code{202}}
158 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
159 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
160 \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
161 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
162 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
163 \lineiii{NO_CONTENT}{\code{204}}
164 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
165 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
166 \lineiii{RESET_CONTENT}{\code{205}}
167 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
168 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
169 \lineiii{PARTIAL_CONTENT}{\code{206}}
170 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
171 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
172 \lineiii{MULTI_STATUS}{\code{207}}
173 {WEBDAV \ulink{RFC 2518, Section 10.2}
Georg Brandl7f26a622005-08-27 17:04:58 +0000174 {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000175 \lineiii{IM_USED}{\code{226}}
176 {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
177
178 \lineiii{MULTIPLE_CHOICES}{\code{300}}
179 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
180 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
181 \lineiii{MOVED_PERMANENTLY}{\code{301}}
182 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
183 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
184 \lineiii{FOUND}{\code{302}}
185 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
186 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
187 \lineiii{SEE_OTHER}{\code{303}}
188 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
189 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
190 \lineiii{NOT_MODIFIED}{\code{304}}
191 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
192 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
193 \lineiii{USE_PROXY}{\code{305}}
194 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
195 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
196 \lineiii{TEMPORARY_REDIRECT}{\code{307}}
197 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
198 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
199
200 \lineiii{BAD_REQUEST}{\code{400}}
201 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
202 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
203 \lineiii{UNAUTHORIZED}{\code{401}}
204 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
205 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
206 \lineiii{PAYMENT_REQUIRED}{\code{402}}
207 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
208 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
209 \lineiii{FORBIDDEN}{\code{403}}
210 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
211 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
212 \lineiii{NOT_FOUND}{\code{404}}
213 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
214 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
215 \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
216 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
217 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
218 \lineiii{NOT_ACCEPTABLE}{\code{406}}
219 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
220 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
221 \lineiii{PROXY_AUTHENTICATION_REQUIRED}
222 {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
223 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
224 \lineiii{REQUEST_TIMEOUT}{\code{408}}
225 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
226 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
227 \lineiii{CONFLICT}{\code{409}}
228 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
229 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
230 \lineiii{GONE}{\code{410}}
231 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
232 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
233 \lineiii{LENGTH_REQUIRED}{\code{411}}
234 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
235 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
236 \lineiii{PRECONDITION_FAILED}{\code{412}}
237 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
238 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
239 \lineiii{REQUEST_ENTITY_TOO_LARGE}
240 {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
241 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
242 \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
243 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
244 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
245 \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
246 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
247 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
248 \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
249 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
250 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
251 \lineiii{EXPECTATION_FAILED}{\code{417}}
252 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
253 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
254 \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
255 {WEBDAV, \ulink{RFC 2518, Section 10.3}
Georg Brandl7f26a622005-08-27 17:04:58 +0000256 {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000257 \lineiii{LOCKED}{\code{423}}
258 {WEBDAV \ulink{RFC 2518, Section 10.4}
Georg Brandl7f26a622005-08-27 17:04:58 +0000259 {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000260 \lineiii{FAILED_DEPENDENCY}{\code{424}}
261 {WEBDAV, \ulink{RFC 2518, Section 10.5}
Georg Brandl7f26a622005-08-27 17:04:58 +0000262 {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000263 \lineiii{UPGRADE_REQUIRED}{\code{426}}
264 {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
265
266 \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
267 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
268 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
269 \lineiii{NOT_IMPLEMENTED}{\code{501}}
270 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
271 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
272 \lineiii{BAD_GATEWAY}{\code{502}}
273 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
274 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
275 \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
276 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
277 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
278 \lineiii{GATEWAY_TIMEOUT}{\code{504}}
279 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
280 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
281 \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
282 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
283 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
284 \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
285 {WEBDAV, \ulink{RFC 2518, Section 10.6}
Georg Brandl7f26a622005-08-27 17:04:58 +0000286 {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000287 \lineiii{NOT_EXTENDED}{\code{510}}
288 {An HTTP Extension Framework, \rfc{2774}, Section 7}
289\end{tableiii}
Fred Drake38f3b722001-11-30 06:06:40 +0000290
291\subsection{HTTPConnection Objects \label{httpconnection-objects}}
292
293\class{HTTPConnection} instances have the following methods:
294
295\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
296This will send a request to the server using the HTTP request method
297\var{method} and the selector \var{url}. If the \var{body} argument is
298present, it should be a string of data to send after the headers are finished.
299The header Content-Length is automatically set to the correct value.
300The \var{headers} argument should be a mapping of extra HTTP headers to send
301with the request.
302\end{methoddesc}
303
304\begin{methoddesc}{getresponse}{}
305Should be called after a request is sent to get the response from the server.
306Returns an \class{HTTPResponse} instance.
Georg Brandl71de0402005-06-25 19:15:48 +0000307\note{Note that you must have read the whole response before you can send a new
308request to the server.}
Fred Drake38f3b722001-11-30 06:06:40 +0000309\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000310
Fred Drakefc576191998-04-04 07:15:02 +0000311\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000312Set the debugging level (the amount of debugging output printed).
313The default debug level is \code{0}, meaning no debugging output is
314printed.
Fred Drakefc576191998-04-04 07:15:02 +0000315\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000316
Fred Drake38f3b722001-11-30 06:06:40 +0000317\begin{methoddesc}{connect}{}
318Connect to the server specified when the object was created.
319\end{methoddesc}
320
321\begin{methoddesc}{close}{}
322Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000323\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000324
Georg Brandl71de0402005-06-25 19:15:48 +0000325As an alternative to using the \method{request()} method described above,
326you can also send your request step by step, by using the four functions
327below.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000328
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000329\begin{methoddesc}{putrequest}{request, selector\optional{,
330skip\_host\optional{, skip_accept_encoding}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000331This should be the first call after the connection to the server has
332been made. It sends a line to the server consisting of the
333\var{request} string, the \var{selector} string, and the HTTP version
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000334(\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
335\code{Accept-Encoding:} headers (for example to accept additional
336content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
337with non-False values.
338\versionchanged[\var{skip_accept_encoding} argument added]{2.4}
Fred Drakefc576191998-04-04 07:15:02 +0000339\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000340
Fred Drakefc576191998-04-04 07:15:02 +0000341\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000342Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000343server consisting of the header, a colon and a space, and the first
344argument. If more arguments are given, continuation lines are sent,
345each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000346\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000347
Fred Drakefc576191998-04-04 07:15:02 +0000348\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000349Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000350\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000351
Georg Brandl71de0402005-06-25 19:15:48 +0000352\begin{methoddesc}{send}{data}
353Send data to the server. This should be used directly only after the
354\method{endheaders()} method has been called and before
355\method{getresponse()} is called.
356\end{methoddesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000357
358\subsection{HTTPResponse Objects \label{httpresponse-objects}}
359
360\class{HTTPResponse} instances have the following methods and attributes:
361
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000362\begin{methoddesc}{read}{\optional{amt}}
363Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000364\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000365
Fred Drake38f3b722001-11-30 06:06:40 +0000366\begin{methoddesc}{getheader}{name\optional{, default}}
367Get the contents of the header \var{name}, or \var{default} if there is no
368matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000369\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000370
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000371\begin{methoddesc}{getheaders}{}
372Return a list of (header, value) tuples. \versionadded{2.4}
373\end{methoddesc}
374
Fred Drake38f3b722001-11-30 06:06:40 +0000375\begin{datadesc}{msg}
376 A \class{mimetools.Message} instance containing the response headers.
377\end{datadesc}
378
379\begin{datadesc}{version}
380 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
381\end{datadesc}
382
383\begin{datadesc}{status}
384 Status code returned by server.
385\end{datadesc}
386
387\begin{datadesc}{reason}
388 Reason phrase returned by server.
389\end{datadesc}
390
Fred Drakec0765c22001-09-25 16:32:02 +0000391
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000392\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000393
Fred Drake4e716fa2000-06-28 21:51:43 +0000394Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000395
Fred Drake19479911998-02-13 06:58:54 +0000396\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000397>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000398>>> conn = httplib.HTTPConnection("www.python.org")
399>>> conn.request("GET", "/index.html")
400>>> r1 = conn.getresponse()
401>>> print r1.status, r1.reason
402200 OK
403>>> data1 = r1.read()
404>>> conn.request("GET", "/parrot.spam")
405>>> r2 = conn.getresponse()
406>>> print r2.status, r2.reason
407404 Not Found
408>>> data2 = r2.read()
409>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000410\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000411
412Here is an example session that shows how to \samp{POST} requests:
413
414\begin{verbatim}
415>>> import httplib, urllib
416>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000417>>> headers = {"Content-type": "application/x-www-form-urlencoded",
418... "Accept": "text/plain"}
419>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
420>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000421>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000422>>> print response.status, response.reason
423200 OK
424>>> data = response.read()
425>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000426\end{verbatim}