blob: 0067a2257c7b9a312c07a4fdb6f7109788c2cf1c [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 Drakefc576191998-04-04 07:15:02 +000016The module defines one class, \class{HTTP}:
17
18\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
19An \class{HTTP} instance
Guido van Rossuma12ef941995-02-27 17:53:25 +000020represents one transaction with an HTTP server. It should be
21instantiated passing it a host and optional port number. If no port
22number is passed, the port is extracted from the host string if it has
Fred Drakea2e98181998-03-12 05:54:02 +000023the form \code{\var{host}:\var{port}}, else the default HTTP port (80)
24is used. If no host is passed, no connection is made, and the
25\method{connect()} method should be used to connect to a server. For
26example, the following calls all create instances that connect to the
27server at the same host and port:
Guido van Rossum470be141995-03-17 16:07:09 +000028
Fred Drake19479911998-02-13 06:58:54 +000029\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000030>>> h1 = httplib.HTTP('www.cwi.nl')
31>>> h2 = httplib.HTTP('www.cwi.nl:80')
32>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000033\end{verbatim}
Fred Drakea2e98181998-03-12 05:54:02 +000034
35Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossuma12ef941995-02-27 17:53:25 +000036should be used as follows:
37
38\begin{enumerate}
39
Fred Drake30bd6662001-11-09 05:03:05 +000040\item Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000041
Fred Drake30bd6662001-11-09 05:03:05 +000042\item Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000043
Fred Drake30bd6662001-11-09 05:03:05 +000044\item Call the \method{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000045step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000046
Fred Drake30bd6662001-11-09 05:03:05 +000047\item Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000048
Fred Drake30bd6662001-11-09 05:03:05 +000049\item Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000050
Fred Drake30bd6662001-11-09 05:03:05 +000051\item Call the \method{getfile()} method and read the data off the
Guido van Rossuma12ef941995-02-27 17:53:25 +000052file object that it returns.
53
54\end{enumerate}
Fred Drakefc576191998-04-04 07:15:02 +000055\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000056
Fred Drake30bd6662001-11-09 05:03:05 +000057\begin{datadesc}{HTTP_PORT}
58 The default port for the HTTP protocol (always \code{80}).
59\end{datadesc}
60
61\begin{datadesc}{HTTPS_PORT}
62 The default port for the HTTPS protocol (always \code{443}).
63\end{datadesc}
64
65
66\subsection{HTTP Objects \label{http-objects}}
Guido van Rossumecde7811995-03-28 13:35:14 +000067
Fred Drakea2e98181998-03-12 05:54:02 +000068\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000069
Guido van Rossumecde7811995-03-28 13:35:14 +000070
Fred Drakefc576191998-04-04 07:15:02 +000071\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +000072Set the debugging level (the amount of debugging output printed).
73The default debug level is \code{0}, meaning no debugging output is
74printed.
Fred Drakefc576191998-04-04 07:15:02 +000075\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000076
Fred Drakefc576191998-04-04 07:15:02 +000077\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000078Connect to the server given by \var{host} and \var{port}. See the
Fred Drake30bd6662001-11-09 05:03:05 +000079introduction to the \refmodule{httplib} module for information on the
80default ports. This should be called directly only if the instance
81was instantiated without passing a host.
Fred Drakefc576191998-04-04 07:15:02 +000082\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000083
Fred Drakefc576191998-04-04 07:15:02 +000084\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +000085Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000086\method{endheaders()} method has been called and before
87\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +000088\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000089
Fred Drakefc576191998-04-04 07:15:02 +000090\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000091This should be the first call after the connection to the server has
92been made. It sends a line to the server consisting of the
93\var{request} string, the \var{selector} string, and the HTTP version
94(\code{HTTP/1.0}).
Fred Drakefc576191998-04-04 07:15:02 +000095\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000096
Fred Drakefc576191998-04-04 07:15:02 +000097\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000098Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000099server consisting of the header, a colon and a space, and the first
100argument. If more arguments are given, continuation lines are sent,
101each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +0000102\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000103
Fred Drakefc576191998-04-04 07:15:02 +0000104\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000105Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +0000106\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000107
Fred Drakefc576191998-04-04 07:15:02 +0000108\begin{methoddesc}{getreply}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000109Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +0000110read the reply from the server, and return a triple
111\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
Fred Drake17765561998-11-30 19:00:16 +0000112\var{replycode} is the integer reply code from the request (e.g.,
Fred Drakea2e98181998-03-12 05:54:02 +0000113\code{200} if the request was handled properly); \var{message} is the
114message string corresponding to the reply code; and \var{headers} is
115an instance of the class \class{mimetools.Message} containing the
116headers received from the server. See the description of the
Fred Drake12a95691999-04-22 16:47:27 +0000117\refmodule{mimetools}\refstmodindex{mimetools} module.
Fred Drakefc576191998-04-04 07:15:02 +0000118\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000119
Fred Drakefc576191998-04-04 07:15:02 +0000120\begin{methoddesc}{getfile}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000121Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000122read, using the \method{read()}, \method{readline()} or
123\method{readlines()} methods.
Fred Drakefc576191998-04-04 07:15:02 +0000124\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000125
Fred Drakec0765c22001-09-25 16:32:02 +0000126
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000127\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000128
Fred Drake4e716fa2000-06-28 21:51:43 +0000129Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000130
Fred Drake19479911998-02-13 06:58:54 +0000131\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000132>>> import httplib
133>>> h = httplib.HTTP('www.cwi.nl')
134>>> h.putrequest('GET', '/index.html')
135>>> h.putheader('Accept', 'text/html')
136>>> h.putheader('Accept', 'text/plain')
Fred Drake481cf2c2001-09-01 02:35:23 +0000137>>> h.putheader('Host', 'www.cwi.nl')
Guido van Rossum470be141995-03-17 16:07:09 +0000138>>> h.endheaders()
139>>> errcode, errmsg, headers = h.getreply()
140>>> print errcode # Should be 200
141>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000142>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000143>>> f.close()
Fred Drake19479911998-02-13 06:58:54 +0000144\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000145
146Here is an example session that shows how to \samp{POST} requests:
147
148\begin{verbatim}
149>>> import httplib, urllib
150>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
151>>> h = httplib.HTTP("www.musi-cal.com:80")
152>>> h.putrequest("POST", "/cgi-bin/query")
Fred Draked99e5342000-11-17 18:04:03 +0000153>>> h.putheader("Content-type", "application/x-www-form-urlencoded")
Fred Drake4e716fa2000-06-28 21:51:43 +0000154>>> h.putheader("Content-length", "%d" % len(params))
155>>> h.putheader('Accept', 'text/plain')
156>>> h.putheader('Host', 'www.musi-cal.com')
157>>> h.endheaders()
Fred Draked99e5342000-11-17 18:04:03 +0000158>>> h.send(params)
Fred Drake4e716fa2000-06-28 21:51:43 +0000159>>> reply, msg, hdrs = h.getreply()
Fred Draked99e5342000-11-17 18:04:03 +0000160>>> print reply # should be 200
Fred Drake4e716fa2000-06-28 21:51:43 +0000161>>> data = h.getfile().read() # get the raw HTML
162\end{verbatim}