blob: 5fd48c1cabb8614c378b96fc3b0e7897811d583d [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
Guido van Rossumd8faa362007-04-27 19:54:29 +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
Guido van Rossumd8faa362007-04-27 19:54:29 +000035used. When True, the optional parameter \var{strict}
36causes \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
39seconds (if it is not given or \code{None}, the global default
40timeout 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)
Guido van Rossumd8faa362007-04-27 19:54:29 +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
Brett Cannon235d1ef2003-05-20 02:56:35 +000054\begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
Fred Drake38f3b722001-11-30 06:06:40 +000055A subclass of \class{HTTPConnection} that uses SSL for communication with
56secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000057\var{key_file} is
58the name of a PEM formatted file that contains your private
59key. \var{cert_file} is a PEM formatted certificate chain file.
60
61\warning{This does not do any certificate verification!}
62
Skip Montanaro13a28632003-01-27 15:00:38 +000063\versionadded{2.0}
64\end{classdesc}
65
66\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
67Class whose instances are returned upon successful connection. Not
68instantiated directly by user.
69\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000070\end{classdesc}
71
72The following exceptions are raised as appropriate:
73
74\begin{excdesc}{HTTPException}
75The base class of the other exceptions in this module. It is a
76subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000077\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000078\end{excdesc}
79
80\begin{excdesc}{NotConnected}
81A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000082\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000083\end{excdesc}
84
Skip Montanaro1e962cb2002-03-24 16:55:57 +000085\begin{excdesc}{InvalidURL}
86A subclass of \exception{HTTPException}, raised if a port is given and is
87either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000088\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000089\end{excdesc}
90
Fred Drake38f3b722001-11-30 06:06:40 +000091\begin{excdesc}{UnknownProtocol}
92A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000093\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000094\end{excdesc}
95
96\begin{excdesc}{UnknownTransferEncoding}
97A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000098\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000099\end{excdesc}
100
101\begin{excdesc}{UnimplementedFileMode}
102A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000103\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000104\end{excdesc}
105
106\begin{excdesc}{IncompleteRead}
107A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000108\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000109\end{excdesc}
110
111\begin{excdesc}{ImproperConnectionState}
112A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000113\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000114\end{excdesc}
115
116\begin{excdesc}{CannotSendRequest}
117A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000118\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000119\end{excdesc}
120
121\begin{excdesc}{CannotSendHeader}
122A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000123\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000124\end{excdesc}
125
126\begin{excdesc}{ResponseNotReady}
127A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000128\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000129\end{excdesc}
130
131\begin{excdesc}{BadStatusLine}
132A subclass of \exception{HTTPException}. Raised if a server responds with a
133HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000134\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000135\end{excdesc}
136
Martin v. Löwis39a31782004-09-18 09:03:49 +0000137The constants defined in this module are:
138
139\begin{datadesc}{HTTP_PORT}
140 The default port for the HTTP protocol (always \code{80}).
141\end{datadesc}
142
143\begin{datadesc}{HTTPS_PORT}
144 The default port for the HTTPS protocol (always \code{443}).
145\end{datadesc}
146
147and also the following constants for integer status codes:
148
149\begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
150 \lineiii{CONTINUE}{\code{100}}
151 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
152 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
153 \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
154 {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
155 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
156 \lineiii{PROCESSING}{\code{102}}
157 {WEBDAV, \ulink{RFC 2518, Section 10.1}
Georg Brandl7f26a622005-08-27 17:04:58 +0000158 {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000159
160 \lineiii{OK}{\code{200}}
161 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
162 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
163 \lineiii{CREATED}{\code{201}}
164 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
165 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
166 \lineiii{ACCEPTED}{\code{202}}
167 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
168 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
169 \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
170 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
171 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
172 \lineiii{NO_CONTENT}{\code{204}}
173 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
174 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
175 \lineiii{RESET_CONTENT}{\code{205}}
176 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
177 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
178 \lineiii{PARTIAL_CONTENT}{\code{206}}
179 {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
180 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
181 \lineiii{MULTI_STATUS}{\code{207}}
182 {WEBDAV \ulink{RFC 2518, Section 10.2}
Georg Brandl7f26a622005-08-27 17:04:58 +0000183 {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000184 \lineiii{IM_USED}{\code{226}}
185 {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
186
187 \lineiii{MULTIPLE_CHOICES}{\code{300}}
188 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
189 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
190 \lineiii{MOVED_PERMANENTLY}{\code{301}}
191 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
192 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
193 \lineiii{FOUND}{\code{302}}
194 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
195 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
196 \lineiii{SEE_OTHER}{\code{303}}
197 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
198 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
199 \lineiii{NOT_MODIFIED}{\code{304}}
200 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
201 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
202 \lineiii{USE_PROXY}{\code{305}}
203 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
204 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
205 \lineiii{TEMPORARY_REDIRECT}{\code{307}}
206 {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
207 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
208
209 \lineiii{BAD_REQUEST}{\code{400}}
210 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
211 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
212 \lineiii{UNAUTHORIZED}{\code{401}}
213 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
214 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
215 \lineiii{PAYMENT_REQUIRED}{\code{402}}
216 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
217 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
218 \lineiii{FORBIDDEN}{\code{403}}
219 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
220 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
221 \lineiii{NOT_FOUND}{\code{404}}
222 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
223 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
224 \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
225 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
226 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
227 \lineiii{NOT_ACCEPTABLE}{\code{406}}
228 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
229 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
230 \lineiii{PROXY_AUTHENTICATION_REQUIRED}
231 {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
232 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
233 \lineiii{REQUEST_TIMEOUT}{\code{408}}
234 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
235 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
236 \lineiii{CONFLICT}{\code{409}}
237 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
238 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
239 \lineiii{GONE}{\code{410}}
240 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
241 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
242 \lineiii{LENGTH_REQUIRED}{\code{411}}
243 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
244 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
245 \lineiii{PRECONDITION_FAILED}{\code{412}}
246 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
247 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
248 \lineiii{REQUEST_ENTITY_TOO_LARGE}
249 {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
250 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
251 \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
252 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
253 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
254 \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
255 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
256 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
257 \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
258 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
259 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
260 \lineiii{EXPECTATION_FAILED}{\code{417}}
261 {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
262 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
263 \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
264 {WEBDAV, \ulink{RFC 2518, Section 10.3}
Georg Brandl7f26a622005-08-27 17:04:58 +0000265 {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000266 \lineiii{LOCKED}{\code{423}}
267 {WEBDAV \ulink{RFC 2518, Section 10.4}
Georg Brandl7f26a622005-08-27 17:04:58 +0000268 {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000269 \lineiii{FAILED_DEPENDENCY}{\code{424}}
270 {WEBDAV, \ulink{RFC 2518, Section 10.5}
Georg Brandl7f26a622005-08-27 17:04:58 +0000271 {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000272 \lineiii{UPGRADE_REQUIRED}{\code{426}}
273 {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
274
275 \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
276 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
277 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
278 \lineiii{NOT_IMPLEMENTED}{\code{501}}
279 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
280 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
281 \lineiii{BAD_GATEWAY}{\code{502}}
282 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
283 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
284 \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
285 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
286 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
287 \lineiii{GATEWAY_TIMEOUT}{\code{504}}
288 {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
289 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
290 \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
291 {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
292 {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
293 \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
294 {WEBDAV, \ulink{RFC 2518, Section 10.6}
Georg Brandl7f26a622005-08-27 17:04:58 +0000295 {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
Martin v. Löwis39a31782004-09-18 09:03:49 +0000296 \lineiii{NOT_EXTENDED}{\code{510}}
297 {An HTTP Extension Framework, \rfc{2774}, Section 7}
298\end{tableiii}
Fred Drake38f3b722001-11-30 06:06:40 +0000299
Georg Brandl6aab16e2006-02-17 19:17:25 +0000300\begin{datadesc}{responses}
301This dictionary maps the HTTP 1.1 status codes to the W3C names.
302
303Example: \code{httplib.responses[httplib.NOT_FOUND]} is \code{'Not Found'}.
304\versionadded{2.5}
305\end{datadesc}
306
307
Fred Drake38f3b722001-11-30 06:06:40 +0000308\subsection{HTTPConnection Objects \label{httpconnection-objects}}
309
310\class{HTTPConnection} instances have the following methods:
311
Guido van Rossumd8faa362007-04-27 19:54:29 +0000312\begin{methoddesc}[HTTPConnection]{request}{method, url\optional{, body\optional{, headers}}}
Fred Drake38f3b722001-11-30 06:06:40 +0000313This will send a request to the server using the HTTP request method
314\var{method} and the selector \var{url}. If the \var{body} argument is
315present, it should be a string of data to send after the headers are finished.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000316Alternatively, it may be an open file object, in which case the
317contents of the file is sent; this file object should support
318\code{fileno()} and \code{read()} methods.
Fred Drake38f3b722001-11-30 06:06:40 +0000319The header Content-Length is automatically set to the correct value.
320The \var{headers} argument should be a mapping of extra HTTP headers to send
321with the request.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000322
323\versionchanged[\var{body} can be a file object]{2.6}
Fred Drake38f3b722001-11-30 06:06:40 +0000324\end{methoddesc}
325
Guido van Rossumd8faa362007-04-27 19:54:29 +0000326\begin{methoddesc}[HTTPConnection]{getresponse}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000327Should be called after a request is sent to get the response from the server.
328Returns an \class{HTTPResponse} instance.
Georg Brandl71de0402005-06-25 19:15:48 +0000329\note{Note that you must have read the whole response before you can send a new
330request to the server.}
Fred Drake38f3b722001-11-30 06:06:40 +0000331\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000332
Guido van Rossumd8faa362007-04-27 19:54:29 +0000333\begin{methoddesc}[HTTPConnection]{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000334Set the debugging level (the amount of debugging output printed).
335The default debug level is \code{0}, meaning no debugging output is
336printed.
Fred Drakefc576191998-04-04 07:15:02 +0000337\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000338
Guido van Rossumd8faa362007-04-27 19:54:29 +0000339\begin{methoddesc}[HTTPConnection]{connect}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000340Connect to the server specified when the object was created.
341\end{methoddesc}
342
Guido van Rossumd8faa362007-04-27 19:54:29 +0000343\begin{methoddesc}[HTTPConnection]{close}{}
Fred Drake38f3b722001-11-30 06:06:40 +0000344Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000345\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000346
Georg Brandl71de0402005-06-25 19:15:48 +0000347As an alternative to using the \method{request()} method described above,
348you can also send your request step by step, by using the four functions
349below.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000350
Guido van Rossumd8faa362007-04-27 19:54:29 +0000351\begin{methoddesc}[HTTPConnection]{putrequest}{request, selector\optional{,
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000352skip\_host\optional{, skip_accept_encoding}}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000353This should be the first call after the connection to the server has
354been made. It sends a line to the server consisting of the
355\var{request} string, the \var{selector} string, and the HTTP version
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000356(\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
357\code{Accept-Encoding:} headers (for example to accept additional
358content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
359with non-False values.
360\versionchanged[\var{skip_accept_encoding} argument added]{2.4}
Fred Drakefc576191998-04-04 07:15:02 +0000361\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000362
Guido van Rossumd8faa362007-04-27 19:54:29 +0000363\begin{methoddesc}[HTTPConnection]{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000364Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000365server consisting of the header, a colon and a space, and the first
366argument. If more arguments are given, continuation lines are sent,
367each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000368\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000369
Guido van Rossumd8faa362007-04-27 19:54:29 +0000370\begin{methoddesc}[HTTPConnection]{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000371Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000372\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000373
Guido van Rossumd8faa362007-04-27 19:54:29 +0000374\begin{methoddesc}[HTTPConnection]{send}{data}
Georg Brandl71de0402005-06-25 19:15:48 +0000375Send data to the server. This should be used directly only after the
376\method{endheaders()} method has been called and before
377\method{getresponse()} is called.
378\end{methoddesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000379
380\subsection{HTTPResponse Objects \label{httpresponse-objects}}
381
382\class{HTTPResponse} instances have the following methods and attributes:
383
Guido van Rossumd8faa362007-04-27 19:54:29 +0000384\begin{methoddesc}[HTTPResponse]{read}{\optional{amt}}
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000385Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000386\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000387
Guido van Rossumd8faa362007-04-27 19:54:29 +0000388\begin{methoddesc}[HTTPResponse]{getheader}{name\optional{, default}}
Fred Drake38f3b722001-11-30 06:06:40 +0000389Get the contents of the header \var{name}, or \var{default} if there is no
390matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000391\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000392
Guido van Rossumd8faa362007-04-27 19:54:29 +0000393\begin{methoddesc}[HTTPResponse]{getheaders}{}
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000394Return a list of (header, value) tuples. \versionadded{2.4}
395\end{methoddesc}
396
Guido van Rossumd8faa362007-04-27 19:54:29 +0000397\begin{memberdesc}[HTTPResponse]{msg}
Fred Drake38f3b722001-11-30 06:06:40 +0000398 A \class{mimetools.Message} instance containing the response headers.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000399\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000400
Guido van Rossumd8faa362007-04-27 19:54:29 +0000401\begin{memberdesc}[HTTPResponse]{version}
Fred Drake38f3b722001-11-30 06:06:40 +0000402 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000403\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000404
Guido van Rossumd8faa362007-04-27 19:54:29 +0000405\begin{memberdesc}[HTTPResponse]{status}
Fred Drake38f3b722001-11-30 06:06:40 +0000406 Status code returned by server.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000407\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000408
Guido van Rossumd8faa362007-04-27 19:54:29 +0000409\begin{memberdesc}[HTTPResponse]{reason}
Fred Drake38f3b722001-11-30 06:06:40 +0000410 Reason phrase returned by server.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000411\end{memberdesc}
Fred Drake38f3b722001-11-30 06:06:40 +0000412
Fred Drakec0765c22001-09-25 16:32:02 +0000413
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000414\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000415
Fred Drake4e716fa2000-06-28 21:51:43 +0000416Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000417
Fred Drake19479911998-02-13 06:58:54 +0000418\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000419>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000420>>> conn = httplib.HTTPConnection("www.python.org")
421>>> conn.request("GET", "/index.html")
422>>> r1 = conn.getresponse()
423>>> print r1.status, r1.reason
424200 OK
425>>> data1 = r1.read()
426>>> conn.request("GET", "/parrot.spam")
427>>> r2 = conn.getresponse()
428>>> print r2.status, r2.reason
429404 Not Found
430>>> data2 = r2.read()
431>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000432\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000433
434Here is an example session that shows how to \samp{POST} requests:
435
436\begin{verbatim}
437>>> import httplib, urllib
438>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000439>>> headers = {"Content-type": "application/x-www-form-urlencoded",
440... "Accept": "text/plain"}
441>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
442>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000443>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000444>>> print response.status, response.reason
445200 OK
446>>> data = response.read()
447>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000448\end{verbatim}