blob: 453000bb9a71e8de699f0630eccf679d35c228c1 [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 Drake0aa811c2001-10-20 04:24:09 +000013that use HTTP and HTTPS. \note{HTTPS support is only
Fred Drakec0765c22001-09-25 16:32:02 +000014available if the \refmodule{socket} module was compiled with SSL
Fred Drake0aa811c2001-10-20 04:24:09 +000015support.}
Guido van Rossuma12ef941995-02-27 17:53:25 +000016
Fred Drake38f3b722001-11-30 06:06:40 +000017The constants defined in this module are:
Guido van Rossuma12ef941995-02-27 17:53:25 +000018
Fred Drake30bd6662001-11-09 05:03:05 +000019\begin{datadesc}{HTTP_PORT}
20 The default port for the HTTP protocol (always \code{80}).
21\end{datadesc}
22
23\begin{datadesc}{HTTPS_PORT}
24 The default port for the HTTPS protocol (always \code{443}).
25\end{datadesc}
26
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}
42\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000043
Fred Drake38f3b722001-11-30 06:06:40 +000044\begin{classdesc}{HTTPSConnection}{host\optional{, port}}
45A subclass of \class{HTTPConnection} that uses SSL for communication with
46secure servers. Default port is \code{443}.
47\end{classdesc}
48
49The following exceptions are raised as appropriate:
50
51\begin{excdesc}{HTTPException}
52The base class of the other exceptions in this module. It is a
53subclass of \exception{Exception}.
54\end{excdesc}
55
56\begin{excdesc}{NotConnected}
57A subclass of \exception{HTTPException}.
58\end{excdesc}
59
Skip Montanaro1e962cb2002-03-24 16:55:57 +000060\begin{excdesc}{InvalidURL}
61A subclass of \exception{HTTPException}, raised if a port is given and is
62either non-numeric or empty.
63\end{excdesc}
64
Fred Drake38f3b722001-11-30 06:06:40 +000065\begin{excdesc}{UnknownProtocol}
66A subclass of \exception{HTTPException}.
67\end{excdesc}
68
69\begin{excdesc}{UnknownTransferEncoding}
70A subclass of \exception{HTTPException}.
71\end{excdesc}
72
73\begin{excdesc}{IllegalKeywordArgument}
74A subclass of \exception{HTTPException}.
75\end{excdesc}
76
77\begin{excdesc}{UnimplementedFileMode}
78A subclass of \exception{HTTPException}.
79\end{excdesc}
80
81\begin{excdesc}{IncompleteRead}
82A subclass of \exception{HTTPException}.
83\end{excdesc}
84
85\begin{excdesc}{ImproperConnectionState}
86A subclass of \exception{HTTPException}.
87\end{excdesc}
88
89\begin{excdesc}{CannotSendRequest}
90A subclass of \exception{ImproperConnectionState}.
91\end{excdesc}
92
93\begin{excdesc}{CannotSendHeader}
94A subclass of \exception{ImproperConnectionState}.
95\end{excdesc}
96
97\begin{excdesc}{ResponseNotReady}
98A subclass of \exception{ImproperConnectionState}.
99\end{excdesc}
100
101\begin{excdesc}{BadStatusLine}
102A subclass of \exception{HTTPException}. Raised if a server responds with a
103HTTP status code that we don't understand.
104\end{excdesc}
105
106
107\subsection{HTTPConnection Objects \label{httpconnection-objects}}
108
109\class{HTTPConnection} instances have the following methods:
110
111\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
112This will send a request to the server using the HTTP request method
113\var{method} and the selector \var{url}. If the \var{body} argument is
114present, it should be a string of data to send after the headers are finished.
115The header Content-Length is automatically set to the correct value.
116The \var{headers} argument should be a mapping of extra HTTP headers to send
117with the request.
118\end{methoddesc}
119
120\begin{methoddesc}{getresponse}{}
121Should be called after a request is sent to get the response from the server.
122Returns an \class{HTTPResponse} instance.
123\end{methoddesc}
Guido van Rossumecde7811995-03-28 13:35:14 +0000124
Fred Drakefc576191998-04-04 07:15:02 +0000125\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000126Set the debugging level (the amount of debugging output printed).
127The default debug level is \code{0}, meaning no debugging output is
128printed.
Fred Drakefc576191998-04-04 07:15:02 +0000129\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000130
Fred Drake38f3b722001-11-30 06:06:40 +0000131\begin{methoddesc}{connect}{}
132Connect to the server specified when the object was created.
133\end{methoddesc}
134
135\begin{methoddesc}{close}{}
136Close the connection to the server.
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}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000140Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +0000141\method{endheaders()} method has been called and before
142\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +0000143\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000144
Fred Drakefc576191998-04-04 07:15:02 +0000145\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000146This should be the first call after the connection to the server has
147been made. It sends a line to the server consisting of the
148\var{request} string, the \var{selector} string, and the HTTP version
Fred Drake38f3b722001-11-30 06:06:40 +0000149(\code{HTTP/1.1}).
Fred Drakefc576191998-04-04 07:15:02 +0000150\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000151
Fred Drakefc576191998-04-04 07:15:02 +0000152\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drake38f3b722001-11-30 06:06:40 +0000153Send an \rfc{822}-style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000154server consisting of the header, a colon and a space, and the first
155argument. If more arguments are given, continuation lines are sent,
156each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000157\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000158
Fred Drakefc576191998-04-04 07:15:02 +0000159\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000160Send a blank line to the server, signalling the end of the headers.
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
164\subsection{HTTPResponse Objects \label{httpresponse-objects}}
165
166\class{HTTPResponse} instances have the following methods and attributes:
167
168\begin{methoddesc}{read}{}
169Reads and returns the response body.
Fred Drakefc576191998-04-04 07:15:02 +0000170\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000171
Fred Drake38f3b722001-11-30 06:06:40 +0000172\begin{methoddesc}{getheader}{name\optional{, default}}
173Get the contents of the header \var{name}, or \var{default} if there is no
174matching header.
Fred Drakefc576191998-04-04 07:15:02 +0000175\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000176
Fred Drake38f3b722001-11-30 06:06:40 +0000177\begin{datadesc}{msg}
178 A \class{mimetools.Message} instance containing the response headers.
179\end{datadesc}
180
181\begin{datadesc}{version}
182 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
183\end{datadesc}
184
185\begin{datadesc}{status}
186 Status code returned by server.
187\end{datadesc}
188
189\begin{datadesc}{reason}
190 Reason phrase returned by server.
191\end{datadesc}
192
Fred Drakec0765c22001-09-25 16:32:02 +0000193
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000194\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000195
Fred Drake4e716fa2000-06-28 21:51:43 +0000196Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000197
Fred Drake19479911998-02-13 06:58:54 +0000198\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000199>>> import httplib
Fred Drake38f3b722001-11-30 06:06:40 +0000200>>> conn = httplib.HTTPConnection("www.python.org")
201>>> conn.request("GET", "/index.html")
202>>> r1 = conn.getresponse()
203>>> print r1.status, r1.reason
204200 OK
205>>> data1 = r1.read()
206>>> conn.request("GET", "/parrot.spam")
207>>> r2 = conn.getresponse()
208>>> print r2.status, r2.reason
209404 Not Found
210>>> data2 = r2.read()
211>>> conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000212\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000213
214Here is an example session that shows how to \samp{POST} requests:
215
216\begin{verbatim}
217>>> import httplib, urllib
218>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Fred Drake38f3b722001-11-30 06:06:40 +0000219>>> headers = {"Content-type": "application/x-www-form-urlencoded",
220... "Accept": "text/plain"}
221>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
222>>> conn.request("POST", "/cgi-bin/query", params, headers)
Fred Drakedce2e112001-12-21 03:52:04 +0000223>>> response = conn.getresponse()
Fred Drake38f3b722001-11-30 06:06:40 +0000224>>> print response.status, response.reason
225200 OK
226>>> data = response.read()
227>>> conn.close()
Fred Drake4e716fa2000-06-28 21:51:43 +0000228\end{verbatim}