blob: 50b4cf2193f0731e1848965239eb12f789e8e466 [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 constants defined in this module are:
Guido van Rossuma12ef941995-02-27 17:53:25 +000028
Fred Drake30bd6662001-11-09 05:03:05 +000029\begin{datadesc}{HTTP_PORT}
30 The default port for the HTTP protocol (always \code{80}).
31\end{datadesc}
32
33\begin{datadesc}{HTTPS_PORT}
34 The default port for the HTTPS protocol (always \code{443}).
35\end{datadesc}
36
Fred Drake38f3b722001-11-30 06:06:40 +000037The module provides the following classes:
Fred Drake30bd6662001-11-09 05:03:05 +000038
Fred Drake38f3b722001-11-30 06:06:40 +000039\begin{classdesc}{HTTPConnection}{host\optional{, port}}
40An \class{HTTPConnection} instance represents one transaction with an HTTP
41server. It should be instantiated passing it a host and optional port number.
42If no port number is passed, the port is extracted from the host string if it
43has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is
44used. For example, the following calls all create instances that connect to
45the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000046
Fred Drake38f3b722001-11-30 06:06:40 +000047\begin{verbatim}
48>>> h1 = httplib.HTTPConnection('www.cwi.nl')
49>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
50>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
51\end{verbatim}
Skip Montanaro13a28632003-01-27 15:00:38 +000052\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000053\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000054
Brett Cannon235d1ef2003-05-20 02:56:35 +000055\begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
Fred Drake38f3b722001-11-30 06:06:40 +000056A subclass of \class{HTTPConnection} that uses SSL for communication with
57secure servers. Default port is \code{443}.
Brett Cannon235d1ef2003-05-20 02:56:35 +000058\var{key_file} is
59the name of a PEM formatted file that contains your private
60key. \var{cert_file} is a PEM formatted certificate chain file.
61
62\warning{This does not do any certificate verification!}
63
Skip Montanaro13a28632003-01-27 15:00:38 +000064\versionadded{2.0}
65\end{classdesc}
66
67\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
68Class whose instances are returned upon successful connection. Not
69instantiated directly by user.
70\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000071\end{classdesc}
72
73The following exceptions are raised as appropriate:
74
75\begin{excdesc}{HTTPException}
76The base class of the other exceptions in this module. It is a
77subclass of \exception{Exception}.
Skip Montanaro13a28632003-01-27 15:00:38 +000078\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000079\end{excdesc}
80
81\begin{excdesc}{NotConnected}
82A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000083\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000084\end{excdesc}
85
Skip Montanaro1e962cb2002-03-24 16:55:57 +000086\begin{excdesc}{InvalidURL}
87A subclass of \exception{HTTPException}, raised if a port is given and is
88either non-numeric or empty.
Skip Montanaro13a28632003-01-27 15:00:38 +000089\versionadded{2.3}
Skip Montanaro1e962cb2002-03-24 16:55:57 +000090\end{excdesc}
91
Fred Drake38f3b722001-11-30 06:06:40 +000092\begin{excdesc}{UnknownProtocol}
93A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000094\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +000095\end{excdesc}
96
97\begin{excdesc}{UnknownTransferEncoding}
98A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +000099\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000100\end{excdesc}
101
102\begin{excdesc}{UnimplementedFileMode}
103A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000104\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000105\end{excdesc}
106
107\begin{excdesc}{IncompleteRead}
108A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000109\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000110\end{excdesc}
111
112\begin{excdesc}{ImproperConnectionState}
113A subclass of \exception{HTTPException}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000114\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000115\end{excdesc}
116
117\begin{excdesc}{CannotSendRequest}
118A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000119\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000120\end{excdesc}
121
122\begin{excdesc}{CannotSendHeader}
123A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000124\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000125\end{excdesc}
126
127\begin{excdesc}{ResponseNotReady}
128A subclass of \exception{ImproperConnectionState}.
Skip Montanaro13a28632003-01-27 15:00:38 +0000129\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000130\end{excdesc}
131
132\begin{excdesc}{BadStatusLine}
133A subclass of \exception{HTTPException}. Raised if a server responds with a
134HTTP status code that we don't understand.
Skip Montanaro13a28632003-01-27 15:00:38 +0000135\versionadded{2.0}
Fred Drake38f3b722001-11-30 06:06:40 +0000136\end{excdesc}
137
138
139\subsection{HTTPConnection Objects \label{httpconnection-objects}}
140
141\class{HTTPConnection} instances have the following methods:
142
143\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
144This will send a request to the server using the HTTP request method
145\var{method} and the selector \var{url}. If the \var{body} argument is
146present, it should be a string of data to send after the headers are finished.
147The header Content-Length is automatically set to the correct value.
148The \var{headers} argument should be a mapping of extra HTTP headers to send
149with the request.
150\end{methoddesc}
151
152\begin{methoddesc}{getresponse}{}
153Should be called after a request is sent to get the response from the server.
154Returns an \class{HTTPResponse} instance.
155\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000156
Fred Drakefc576191998-04-04 07:15:02 +0000157\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000158Set the debugging level (the amount of debugging output printed).
159The default debug level is \code{0}, meaning no debugging output is
160printed.
Fred Drakefc576191998-04-04 07:15:02 +0000161\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000162
Fred Drake38f3b722001-11-30 06:06:40 +0000163\begin{methoddesc}{connect}{}
164Connect to the server specified when the object was created.
165\end{methoddesc}
166
167\begin{methoddesc}{close}{}
168Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000169\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000170
Fred Drakefc576191998-04-04 07:15:02 +0000171\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000172Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +0000173\method{endheaders()} method has been called and before
174\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +0000175\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000176
Fred Drakefc576191998-04-04 07:15:02 +0000177\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000178This should be the first call after the connection to the server has
179been made. It sends a line to the server consisting of the
180\var{request} string, the \var{selector} string, and the HTTP version
Fred Drake38f3b722001-11-30 06:06:40 +0000181(\code{HTTP/1.1}).
Fred Drakefc576191998-04-04 07:15:02 +0000182\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000183
Fred Drakefc576191998-04-04 07:15:02 +0000184\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000185Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000186server consisting of the header, a colon and a space, and the first
187argument. If more arguments are given, continuation lines are sent,
188each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000189\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000190
Fred Drakefc576191998-04-04 07:15:02 +0000191\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000192Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000193\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000194
Fred Drake38f3b722001-11-30 06:06:40 +0000195
196\subsection{HTTPResponse Objects \label{httpresponse-objects}}
197
198\class{HTTPResponse} instances have the following methods and attributes:
199
Raymond Hettinger09c7b602003-09-02 02:32:54 +0000200\begin{methoddesc}{read}{\optional{amt}}
201Reads and returns the response body, or up to the next \var{amt} bytes.
Fred Drakefc576191998-04-04 07:15:02 +0000202\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000203
Fred Drake38f3b722001-11-30 06:06:40 +0000204\begin{methoddesc}{getheader}{name\optional{, default}}
205Get the contents of the header \var{name}, or \var{default} if there is no
206matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000207\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000208
Fred Drake38f3b722001-11-30 06:06:40 +0000209\begin{datadesc}{msg}
210 A \class{mimetools.Message} instance containing the response headers.
211\end{datadesc}
212
213\begin{datadesc}{version}
214 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
215\end{datadesc}
216
217\begin{datadesc}{status}
218 Status code returned by server.
219\end{datadesc}
220
221\begin{datadesc}{reason}
222 Reason phrase returned by server.
223\end{datadesc}
224
Fred Drakec0765c22001-09-25 16:32:02 +0000225
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000226\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000227
Fred Drake4e716fa2000-06-28 21:51:43 +0000228Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000229
Fred Drake19479911998-02-13 06:58:54 +0000230\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000231>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000232>>> conn = httplib.HTTPConnection("www.python.org")
233>>> conn.request("GET", "/index.html")
234>>> r1 = conn.getresponse()
235>>> print r1.status, r1.reason
236200 OK
237>>> data1 = r1.read()
238>>> conn.request("GET", "/parrot.spam")
239>>> r2 = conn.getresponse()
240>>> print r2.status, r2.reason
241404 Not Found
242>>> data2 = r2.read()
243>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000244\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000245
246Here is an example session that shows how to \samp{POST} requests:
247
248\begin{verbatim}
249>>> import httplib, urllib
250>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000251>>> headers = {"Content-type": "application/x-www-form-urlencoded",
252... "Accept": "text/plain"}
253>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
254>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000255>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000256>>> print response.status, response.reason
257200 OK
258>>> data = response.read()
259>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000260\end{verbatim}