blob: 7c9449de174ca2e7d66f15c1e54955c99781ca61 [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
Georg Brandlf03facf2007-03-26 20:28:28 +000035used. When True, the optional parameter \var{strict}
Facundo Batista07c78be2007-03-23 18:54:07 +000036causes \code{BadStatusLine} to be raised if the status line can't be parsed
37as a valid HTTP/1.0 or 1.1 status line. If the optional \var{timeout}
38parameter is given, connection attempts will timeout after that many
Georg Brandlf03facf2007-03-26 20:28:28 +000039seconds (if it is not given or \code{None}, the global default
Facundo Batista07c78be2007-03-23 18:54:07 +000040timeout setting is used).
41
42For example, the following calls all create instances that connect to
Fred Drake38f3b722001-11-30 06:06:40 +000043the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000044
Fred Drake38f3b722001-11-30 06:06:40 +000045\begin{verbatim}
46>>> h1 = httplib.HTTPConnection('www.cwi.nl')
47>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
48>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
Facundo Batista07c78be2007-03-23 18:54:07 +000049>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)
Fred Drake38f3b722001-11-30 06:06:40 +000050\end{verbatim}
Skip Montanaro13a28632003-01-27 15:00:38 +000051\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000052\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000053
Neal Norwitz6f145fc2007-05-22 06:03:36 +000054\begin{classdesc}{HTTPSConnection}{host\optional{, port\optional{,
55 key_file\optional{, cert_file\optional{,
56 strict\optional{, timeout}}}}}}
Fred Drake38f3b722001-11-30 06:06:40 +000057A subclass of \class{HTTPConnection} that uses SSL for communication with
58secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000059\var{key_file} is
60the name of a PEM formatted file that contains your private
61key. \var{cert_file} is a PEM formatted certificate chain file.
62
63\warning{This does not do any certificate verification!}
64
Skip Montanaro13a28632003-01-27 15:00:38 +000065\versionadded{2.0}
66\end{classdesc}
67
68\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
69Class whose instances are returned upon successful connection. Not
70instantiated directly by user.
71\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000072\end{classdesc}
73
74The following exceptions are raised as appropriate:
75
76\begin{excdesc}{HTTPException}
77The base class of the other exceptions in this module. It is a
78subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000079\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000080\end{excdesc}
81
82\begin{excdesc}{NotConnected}
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
Skip Montanaro1e962cb2002-03-24 16:55:57 +000087\begin{excdesc}{InvalidURL}
88A subclass of \exception{HTTPException}, raised if a port is given and is
89either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000090\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000091\end{excdesc}
92
Fred Drake38f3b722001-11-30 06:06:40 +000093\begin{excdesc}{UnknownProtocol}
94A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000095\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000096\end{excdesc}
97
98\begin{excdesc}{UnknownTransferEncoding}
99A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000100\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000101\end{excdesc}
102
103\begin{excdesc}{UnimplementedFileMode}
104A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000105\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000106\end{excdesc}
107
108\begin{excdesc}{IncompleteRead}
109A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000110\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000111\end{excdesc}
112
113\begin{excdesc}{ImproperConnectionState}
114A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000115\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000116\end{excdesc}
117
118\begin{excdesc}{CannotSendRequest}
119A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000120\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000121\end{excdesc}
122
123\begin{excdesc}{CannotSendHeader}
124A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000125\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000126\end{excdesc}
127
128\begin{excdesc}{ResponseNotReady}
129A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000130\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000131\end{excdesc}
132
133\begin{excdesc}{BadStatusLine}
134A subclass of \exception{HTTPException}. Raised if a server responds with a
135HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000136\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000137\end{excdesc}
138
Martin v. Löwis39a31782004-09-18 09:03:49 +0000139The constants defined in this module are:
140
141\begin{datadesc}{HTTP_PORT}
142 The default port for the HTTP protocol (always \code{80}).
143\end{datadesc}
144
145\begin{datadesc}{HTTPS_PORT}
146 The default port for the HTTPS protocol (always \code{443}).
147\end{datadesc}
148
149and also the following constants for integer status codes:
150
151\begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
152 \lineiii{CONTINUE}{\code{100}}
153 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
154 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
155 \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
156 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
157 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
158 \lineiii{PROCESSING}{\code{102}}
159 {WEBDAV, \ulink{RFC 2518, Section 10.1}
Georg Brandl7f26a622005-08-27 17:04:58 +0000160 {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000161
162 \lineiii{OK}{\code{200}}
163 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
164 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
165 \lineiii{CREATED}{\code{201}}
166 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
167 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
168 \lineiii{ACCEPTED}{\code{202}}
169 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
170 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
171 \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
172 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
173 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
174 \lineiii{NO_CONTENT}{\code{204}}
175 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
176 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
177 \lineiii{RESET_CONTENT}{\code{205}}
178 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
179 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
180 \lineiii{PARTIAL_CONTENT}{\code{206}}
181 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
182 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
183 \lineiii{MULTI_STATUS}{\code{207}}
184 {WEBDAV \ulink{RFC 2518, Section 10.2}
Georg Brandl7f26a622005-08-27 17:04:58 +0000185 {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000186 \lineiii{IM_USED}{\code{226}}
187 {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
188
189 \lineiii{MULTIPLE_CHOICES}{\code{300}}
190 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
191 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
192 \lineiii{MOVED_PERMANENTLY}{\code{301}}
193 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
194 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
195 \lineiii{FOUND}{\code{302}}
196 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
197 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
198 \lineiii{SEE_OTHER}{\code{303}}
199 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
200 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
201 \lineiii{NOT_MODIFIED}{\code{304}}
202 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
203 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
204 \lineiii{USE_PROXY}{\code{305}}
205 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
206 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
207 \lineiii{TEMPORARY_REDIRECT}{\code{307}}
208 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
209 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
210
211 \lineiii{BAD_REQUEST}{\code{400}}
212 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
213 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
214 \lineiii{UNAUTHORIZED}{\code{401}}
215 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
216 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
217 \lineiii{PAYMENT_REQUIRED}{\code{402}}
218 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
219 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
220 \lineiii{FORBIDDEN}{\code{403}}
221 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
222 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
223 \lineiii{NOT_FOUND}{\code{404}}
224 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
225 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
226 \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
227 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
228 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
229 \lineiii{NOT_ACCEPTABLE}{\code{406}}
230 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
231 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
232 \lineiii{PROXY_AUTHENTICATION_REQUIRED}
233 {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
234 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
235 \lineiii{REQUEST_TIMEOUT}{\code{408}}
236 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
237 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
238 \lineiii{CONFLICT}{\code{409}}
239 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
240 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
241 \lineiii{GONE}{\code{410}}
242 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
243 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
244 \lineiii{LENGTH_REQUIRED}{\code{411}}
245 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
246 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
247 \lineiii{PRECONDITION_FAILED}{\code{412}}
248 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
249 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
250 \lineiii{REQUEST_ENTITY_TOO_LARGE}
251 {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
252 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
253 \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
254 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
255 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
256 \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
257 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
258 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
259 \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
260 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
261 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
262 \lineiii{EXPECTATION_FAILED}{\code{417}}
263 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
264 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
265 \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
266 {WEBDAV, \ulink{RFC 2518, Section 10.3}
Georg Brandl7f26a622005-08-27 17:04:58 +0000267 {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000268 \lineiii{LOCKED}{\code{423}}
269 {WEBDAV \ulink{RFC 2518, Section 10.4}
Georg Brandl7f26a622005-08-27 17:04:58 +0000270 {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000271 \lineiii{FAILED_DEPENDENCY}{\code{424}}
272 {WEBDAV, \ulink{RFC 2518, Section 10.5}
Georg Brandl7f26a622005-08-27 17:04:58 +0000273 {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000274 \lineiii{UPGRADE_REQUIRED}{\code{426}}
275 {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
276
277 \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
278 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
279 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
280 \lineiii{NOT_IMPLEMENTED}{\code{501}}
281 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
282 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
283 \lineiii{BAD_GATEWAY}{\code{502}}
284 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
285 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
286 \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
287 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
288 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
289 \lineiii{GATEWAY_TIMEOUT}{\code{504}}
290 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
291 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
292 \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
293 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
294 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
295 \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
296 {WEBDAV, \ulink{RFC 2518, Section 10.6}
Georg Brandl7f26a622005-08-27 17:04:58 +0000297 {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000298 \lineiii{NOT_EXTENDED}{\code{510}}
299 {An HTTP Extension Framework, \rfc{2774}, Section 7}
300\end{tableiii}
Fred Drake38f3b722001-11-30 06:06:40 +0000301
Georg Brandl6aab16e2006-02-17 19:17:25 +0000302\begin{datadesc}{responses}
303This dictionary maps the HTTP 1.1 status codes to the W3C names.
304
305Example: \code{httplib.responses[httplib.NOT_FOUND]} is \code{'Not Found'}.
306\versionadded{2.5}
307\end{datadesc}
308
309
Fred Drake38f3b722001-11-30 06:06:40 +0000310\subsection{HTTPConnection Objects \label{httpconnection-objects}}
311
312\class{HTTPConnection} instances have the following methods:
313
Georg Brandlae91afd2007-04-01 22:39:10 +0000314\begin{methoddesc}[HTTPConnection]{request}{method, url\optional{, body\optional{, headers}}}
Fred Drake38f3b722001-11-30 06:06:40 +0000315This will send a request to the server using the HTTP request method
316\var{method} and the selector \var{url}. If the \var{body} argument is
317present, it should be a string of data to send after the headers are finished.
Martin v. Löwis040a9272006-11-12 10:32:47 +0000318Alternatively, it may be an open file object, in which case the
319contents of the file is sent; this file object should support
320\code{fileno()} and \code{read()} methods.
Fred Drake38f3b722001-11-30 06:06:40 +0000321The header Content-Length is automatically set to the correct value.
322The \var{headers} argument should be a mapping of extra HTTP headers to send
323with the request.
Martin v. Löwis040a9272006-11-12 10:32:47 +0000324
325\versionchanged[\var{body} can be a file object]{2.6}
Fred Drake38f3b722001-11-30 06:06:40 +0000326\end{methoddesc}
327
Georg Brandlae91afd2007-04-01 22:39:10 +0000328\begin{methoddesc}[HTTPConnection]{getresponse}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000329Should be called after a request is sent to get the response from the server.
330Returns an \class{HTTPResponse} instance.
Georg Brandl71de0402005-06-25 19:15:48 +0000331\note{Note that you must have read the whole response before you can send a new
332request to the server.}
Fred Drake38f3b722001-11-30 06:06:40 +0000333\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000334
Georg Brandlae91afd2007-04-01 22:39:10 +0000335\begin{methoddesc}[HTTPConnection]{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000336Set the debugging level (the amount of debugging output printed).
337The default debug level is \code{0}, meaning no debugging output is
338printed.
Fred Drakefc576191998-04-04 07:15:02 +0000339\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000340
Georg Brandlae91afd2007-04-01 22:39:10 +0000341\begin{methoddesc}[HTTPConnection]{connect}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000342Connect to the server specified when the object was created.
343\end{methoddesc}
344
Georg Brandlae91afd2007-04-01 22:39:10 +0000345\begin{methoddesc}[HTTPConnection]{close}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000346Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000347\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000348
Georg Brandl71de0402005-06-25 19:15:48 +0000349As an alternative to using the \method{request()} method described above,
350you can also send your request step by step, by using the four functions
351below.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000352
Georg Brandlae91afd2007-04-01 22:39:10 +0000353\begin{methoddesc}[HTTPConnection]{putrequest}{request, selector\optional{,
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000354skip\_host\optional{, skip_accept_encoding}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000355This should be the first call after the connection to the server has
356been made. It sends a line to the server consisting of the
357\var{request} string, the \var{selector} string, and the HTTP version
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000358(\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
359\code{Accept-Encoding:} headers (for example to accept additional
360content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
361with non-False values.
362\versionchanged[\var{skip_accept_encoding} argument added]{2.4}
Fred Drakefc576191998-04-04 07:15:02 +0000363\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000364
Georg Brandlae91afd2007-04-01 22:39:10 +0000365\begin{methoddesc}[HTTPConnection]{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000366Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000367server consisting of the header, a colon and a space, and the first
368argument. If more arguments are given, continuation lines are sent,
369each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000370\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000371
Georg Brandlae91afd2007-04-01 22:39:10 +0000372\begin{methoddesc}[HTTPConnection]{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000373Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000374\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000375
Georg Brandlae91afd2007-04-01 22:39:10 +0000376\begin{methoddesc}[HTTPConnection]{send}{data}
Georg Brandl71de0402005-06-25 19:15:48 +0000377Send data to the server. This should be used directly only after the
378\method{endheaders()} method has been called and before
379\method{getresponse()} is called.
380\end{methoddesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000381
382\subsection{HTTPResponse Objects \label{httpresponse-objects}}
383
384\class{HTTPResponse} instances have the following methods and attributes:
385
Georg Brandlae91afd2007-04-01 22:39:10 +0000386\begin{methoddesc}[HTTPResponse]{read}{\optional{amt}}
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000387Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000388\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000389
Georg Brandlae91afd2007-04-01 22:39:10 +0000390\begin{methoddesc}[HTTPResponse]{getheader}{name\optional{, default}}
Fred Drake38f3b722001-11-30 06:06:40 +0000391Get the contents of the header \var{name}, or \var{default} if there is no
392matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000393\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000394
Georg Brandlae91afd2007-04-01 22:39:10 +0000395\begin{methoddesc}[HTTPResponse]{getheaders}{}
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000396Return a list of (header, value) tuples. \versionadded{2.4}
397\end{methoddesc}
398
Georg Brandlae91afd2007-04-01 22:39:10 +0000399\begin{memberdesc}[HTTPResponse]{msg}
Fred Drake38f3b722001-11-30 06:06:40 +0000400 A \class{mimetools.Message} instance containing the response headers.
Georg Brandlae91afd2007-04-01 22:39:10 +0000401\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000402
Georg Brandlae91afd2007-04-01 22:39:10 +0000403\begin{memberdesc}[HTTPResponse]{version}
Fred Drake38f3b722001-11-30 06:06:40 +0000404 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
Georg Brandlae91afd2007-04-01 22:39:10 +0000405\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000406
Georg Brandlae91afd2007-04-01 22:39:10 +0000407\begin{memberdesc}[HTTPResponse]{status}
Fred Drake38f3b722001-11-30 06:06:40 +0000408 Status code returned by server.
Georg Brandlae91afd2007-04-01 22:39:10 +0000409\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000410
Georg Brandlae91afd2007-04-01 22:39:10 +0000411\begin{memberdesc}[HTTPResponse]{reason}
Fred Drake38f3b722001-11-30 06:06:40 +0000412 Reason phrase returned by server.
Georg Brandlae91afd2007-04-01 22:39:10 +0000413\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000414
Fred Drakec0765c22001-09-25 16:32:02 +0000415
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000416\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000417
Fred Drake4e716fa2000-06-28 21:51:43 +0000418Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000419
Fred Drake19479911998-02-13 06:58:54 +0000420\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000421>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000422>>> conn = httplib.HTTPConnection("www.python.org")
423>>> conn.request("GET", "/index.html")
424>>> r1 = conn.getresponse()
425>>> print r1.status, r1.reason
426200 OK
427>>> data1 = r1.read()
428>>> conn.request("GET", "/parrot.spam")
429>>> r2 = conn.getresponse()
430>>> print r2.status, r2.reason
431404 Not Found
432>>> data2 = r2.read()
433>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000434\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000435
436Here is an example session that shows how to \samp{POST} requests:
437
438\begin{verbatim}
439>>> import httplib, urllib
440>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000441>>> headers = {"Content-type": "application/x-www-form-urlencoded",
442... "Accept": "text/plain"}
443>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
444>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000445>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000446>>> print response.status, response.reason
447200 OK
448>>> data = response.read()
449>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000450\end{verbatim}