blob: cfe008bdd096e7328a9b2c36273b3a3eee808d0f [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}
Guido van Rossuma12ef941995-02-27 17:53:25 +00008
Fred Drakec0765c22001-09-25 16:32:02 +00009This module defines classes which implement the client side of the
10HTTP and HTTPS protocols. It is normally not used directly --- the
11module \refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs
Fred Drake0aa811c2001-10-20 04:24:09 +000012that use HTTP and HTTPS. \note{HTTPS support is only
Fred Drakec0765c22001-09-25 16:32:02 +000013available if the \refmodule{socket} module was compiled with SSL
Fred Drake0aa811c2001-10-20 04:24:09 +000014support.}
Guido van Rossuma12ef941995-02-27 17:53:25 +000015
Fred Drake38f3b722001-11-30 06:06:40 +000016The constants defined in this module are:
Guido van Rossuma12ef941995-02-27 17:53:25 +000017
Fred Drake30bd6662001-11-09 05:03:05 +000018\begin{datadesc}{HTTP_PORT}
19 The default port for the HTTP protocol (always \code{80}).
20\end{datadesc}
21
22\begin{datadesc}{HTTPS_PORT}
23 The default port for the HTTPS protocol (always \code{443}).
24\end{datadesc}
25
Fred Drake38f3b722001-11-30 06:06:40 +000026The module provides the following classes:
Fred Drake30bd6662001-11-09 05:03:05 +000027
Fred Drake38f3b722001-11-30 06:06:40 +000028\begin{classdesc}{HTTPConnection}{host\optional{, port}}
29An \class{HTTPConnection} instance represents one transaction with an HTTP
30server. It should be instantiated passing it a host and optional port number.
31If no port number is passed, the port is extracted from the host string if it
32has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is
33used. For example, the following calls all create instances that connect to
34the server at the same host and port:
Guido van Rossumecde7811995-03-28 13:35:14 +000035
Fred Drake38f3b722001-11-30 06:06:40 +000036\begin{verbatim}
37>>> h1 = httplib.HTTPConnection('www.cwi.nl')
38>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
39>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
40\end{verbatim}
41\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000042
Fred Drake38f3b722001-11-30 06:06:40 +000043\begin{classdesc}{HTTPSConnection}{host\optional{, port}}
44A subclass of \class{HTTPConnection} that uses SSL for communication with
45secure servers. Default port is \code{443}.
46\end{classdesc}
47
48The following exceptions are raised as appropriate:
49
50\begin{excdesc}{HTTPException}
51The base class of the other exceptions in this module. It is a
52subclass of \exception{Exception}.
53\end{excdesc}
54
55\begin{excdesc}{NotConnected}
56A subclass of \exception{HTTPException}.
57\end{excdesc}
58
59\begin{excdesc}{UnknownProtocol}
60A subclass of \exception{HTTPException}.
61\end{excdesc}
62
63\begin{excdesc}{UnknownTransferEncoding}
64A subclass of \exception{HTTPException}.
65\end{excdesc}
66
67\begin{excdesc}{IllegalKeywordArgument}
68A subclass of \exception{HTTPException}.
69\end{excdesc}
70
71\begin{excdesc}{UnimplementedFileMode}
72A subclass of \exception{HTTPException}.
73\end{excdesc}
74
75\begin{excdesc}{IncompleteRead}
76A subclass of \exception{HTTPException}.
77\end{excdesc}
78
79\begin{excdesc}{ImproperConnectionState}
80A subclass of \exception{HTTPException}.
81\end{excdesc}
82
83\begin{excdesc}{CannotSendRequest}
84A subclass of \exception{ImproperConnectionState}.
85\end{excdesc}
86
87\begin{excdesc}{CannotSendHeader}
88A subclass of \exception{ImproperConnectionState}.
89\end{excdesc}
90
91\begin{excdesc}{ResponseNotReady}
92A subclass of \exception{ImproperConnectionState}.
93\end{excdesc}
94
95\begin{excdesc}{BadStatusLine}
96A subclass of \exception{HTTPException}. Raised if a server responds with a
97HTTP status code that we don't understand.
98\end{excdesc}
99
100
101\subsection{HTTPConnection Objects \label{httpconnection-objects}}
102
103\class{HTTPConnection} instances have the following methods:
104
105\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
106This will send a request to the server using the HTTP request method
107\var{method} and the selector \var{url}. If the \var{body} argument is
108present, it should be a string of data to send after the headers are finished.
109The header Content-Length is automatically set to the correct value.
110The \var{headers} argument should be a mapping of extra HTTP headers to send
111with the request.
112\end{methoddesc}
113
114\begin{methoddesc}{getresponse}{}
115Should be called after a request is sent to get the response from the server.
116Returns an \class{HTTPResponse} instance.
117\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000118
Fred Drakefc576191998-04-04 07:15:02 +0000119\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000120Set the debugging level (the amount of debugging output printed).
121The default debug level is \code{0}, meaning no debugging output is
122printed.
Fred Drakefc576191998-04-04 07:15:02 +0000123\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000124
Fred Drake38f3b722001-11-30 06:06:40 +0000125\begin{methoddesc}{connect}{}
126Connect to the server specified when the object was created.
127\end{methoddesc}
128
129\begin{methoddesc}{close}{}
130Close the connection to the server.
Fred Drakefc576191998-04-04 07:15:02 +0000131\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000132
Fred Drakefc576191998-04-04 07:15:02 +0000133\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000134Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +0000135\method{endheaders()} method has been called and before
136\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +0000137\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000138
Fred Drakefc576191998-04-04 07:15:02 +0000139\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000140This should be the first call after the connection to the server has
141been made. It sends a line to the server consisting of the
142\var{request} string, the \var{selector} string, and the HTTP version
Fred Drake38f3b722001-11-30 06:06:40 +0000143(\code{HTTP/1.1}).
Fred Drakefc576191998-04-04 07:15:02 +0000144\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000145
Fred Drakefc576191998-04-04 07:15:02 +0000146\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000147Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000148server consisting of the header, a colon and a space, and the first
149argument. If more arguments are given, continuation lines are sent,
150each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000151\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000152
Fred Drakefc576191998-04-04 07:15:02 +0000153\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000154Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000155\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000156
Fred Drake38f3b722001-11-30 06:06:40 +0000157
158\subsection{HTTPResponse Objects \label{httpresponse-objects}}
159
160\class{HTTPResponse} instances have the following methods and attributes:
161
162\begin{methoddesc}{read}{}
163Reads and returns the response body.
Fred Drakefc576191998-04-04 07:15:02 +0000164\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000165
Fred Drake38f3b722001-11-30 06:06:40 +0000166\begin{methoddesc}{getheader}{name\optional{, default}}
167Get the contents of the header \var{name}, or \var{default} if there is no
168matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000169\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000170
Fred Drake38f3b722001-11-30 06:06:40 +0000171\begin{datadesc}{msg}
172 A \class{mimetools.Message} instance containing the response headers.
173\end{datadesc}
174
175\begin{datadesc}{version}
176 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
177\end{datadesc}
178
179\begin{datadesc}{status}
180 Status code returned by server.
181\end{datadesc}
182
183\begin{datadesc}{reason}
184 Reason phrase returned by server.
185\end{datadesc}
186
Fred Drakec0765c22001-09-25 16:32:02 +0000187
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000188\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000189
Fred Drake4e716fa2000-06-28 21:51:43 +0000190Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000191
Fred Drake19479911998-02-13 06:58:54 +0000192\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000193>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000194>>> conn = httplib.HTTPConnection("www.python.org")
195>>> conn.request("GET", "/index.html")
196>>> r1 = conn.getresponse()
197>>> print r1.status, r1.reason
198200 OK
199>>> data1 = r1.read()
200>>> conn.request("GET", "/parrot.spam")
201>>> r2 = conn.getresponse()
202>>> print r2.status, r2.reason
203404 Not Found
204>>> data2 = r2.read()
205>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000206\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000207
208Here is an example session that shows how to \samp{POST} requests:
209
210\begin{verbatim}
211>>> import httplib, urllib
212>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000213>>> headers = {"Content-type": "application/x-www-form-urlencoded",
214... "Accept": "text/plain"}
215>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
216>>> conn.request("POST", "/cgi-bin/query", params, headers)
217>>> response = h.getresponse()
218>>> print response.status, response.reason
219200 OK
220>>> data = response.read()
221>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000222\end{verbatim}