blob: 6808ef44db28b790f914fad56f33dfa6d8989f8e [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
12that use HTTP and HTTPS. \strong{Note:} HTTPS support is only
13available if the \refmodule{socket} module was compiled with SSL
14support.
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 Drakea2e98181998-03-12 05:54:02 +000040\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000041
Fred Drakea2e98181998-03-12 05:54:02 +000042\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000043
Fred Drakea2e98181998-03-12 05:54:02 +000044\item[3.] 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 Drakea2e98181998-03-12 05:54:02 +000047\item[4.] Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000048
Fred Drakea2e98181998-03-12 05:54:02 +000049\item[5.] Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000050
Fred Drakea2e98181998-03-12 05:54:02 +000051\item[6.] 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
Guido van Rossumecde7811995-03-28 13:35:14 +000057\subsection{HTTP Objects}
58
Fred Drakea2e98181998-03-12 05:54:02 +000059\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000060
Guido van Rossumecde7811995-03-28 13:35:14 +000061
Fred Drakefc576191998-04-04 07:15:02 +000062\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +000063Set the debugging level (the amount of debugging output printed).
64The default debug level is \code{0}, meaning no debugging output is
65printed.
Fred Drakefc576191998-04-04 07:15:02 +000066\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000067
Fred Drakefc576191998-04-04 07:15:02 +000068\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000069Connect to the server given by \var{host} and \var{port}. See the
70intro for the default port. This should be called directly only if
71the instance was instantiated without passing a host.
Fred Drakefc576191998-04-04 07:15:02 +000072\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000073
Fred Drakefc576191998-04-04 07:15:02 +000074\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +000075Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000076\method{endheaders()} method has been called and before
77\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +000078\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000079
Fred Drakefc576191998-04-04 07:15:02 +000080\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000081This should be the first call after the connection to the server has
82been made. It sends a line to the server consisting of the
83\var{request} string, the \var{selector} string, and the HTTP version
84(\code{HTTP/1.0}).
Fred Drakefc576191998-04-04 07:15:02 +000085\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000086
Fred Drakefc576191998-04-04 07:15:02 +000087\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000088Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000089server consisting of the header, a colon and a space, and the first
90argument. If more arguments are given, continuation lines are sent,
91each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +000092\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000093
Fred Drakefc576191998-04-04 07:15:02 +000094\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000095Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +000096\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000097
Fred Drakefc576191998-04-04 07:15:02 +000098\begin{methoddesc}{getreply}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000099Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +0000100read the reply from the server, and return a triple
101\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
Fred Drake17765561998-11-30 19:00:16 +0000102\var{replycode} is the integer reply code from the request (e.g.,
Fred Drakea2e98181998-03-12 05:54:02 +0000103\code{200} if the request was handled properly); \var{message} is the
104message string corresponding to the reply code; and \var{headers} is
105an instance of the class \class{mimetools.Message} containing the
106headers received from the server. See the description of the
Fred Drake12a95691999-04-22 16:47:27 +0000107\refmodule{mimetools}\refstmodindex{mimetools} module.
Fred Drakefc576191998-04-04 07:15:02 +0000108\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000109
Fred Drakefc576191998-04-04 07:15:02 +0000110\begin{methoddesc}{getfile}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000111Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000112read, using the \method{read()}, \method{readline()} or
113\method{readlines()} methods.
Fred Drakefc576191998-04-04 07:15:02 +0000114\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000115
Fred Drakec0765c22001-09-25 16:32:02 +0000116
Fred Drakeef8cd7c2001-01-22 17:42:32 +0000117\subsection{Examples \label{httplib-examples}}
Guido van Rossum470be141995-03-17 16:07:09 +0000118
Fred Drake4e716fa2000-06-28 21:51:43 +0000119Here is an example session that uses the \samp{GET} method:
Guido van Rossum470be141995-03-17 16:07:09 +0000120
Fred Drake19479911998-02-13 06:58:54 +0000121\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000122>>> import httplib
123>>> h = httplib.HTTP('www.cwi.nl')
124>>> h.putrequest('GET', '/index.html')
125>>> h.putheader('Accept', 'text/html')
126>>> h.putheader('Accept', 'text/plain')
Fred Drake481cf2c2001-09-01 02:35:23 +0000127>>> h.putheader('Host', 'www.cwi.nl')
Guido van Rossum470be141995-03-17 16:07:09 +0000128>>> h.endheaders()
129>>> errcode, errmsg, headers = h.getreply()
130>>> print errcode # Should be 200
131>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000132>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000133>>> f.close()
Fred Drake19479911998-02-13 06:58:54 +0000134\end{verbatim}
Fred Drake4e716fa2000-06-28 21:51:43 +0000135
136Here is an example session that shows how to \samp{POST} requests:
137
138\begin{verbatim}
139>>> import httplib, urllib
140>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
141>>> h = httplib.HTTP("www.musi-cal.com:80")
142>>> h.putrequest("POST", "/cgi-bin/query")
Fred Draked99e5342000-11-17 18:04:03 +0000143>>> h.putheader("Content-type", "application/x-www-form-urlencoded")
Fred Drake4e716fa2000-06-28 21:51:43 +0000144>>> h.putheader("Content-length", "%d" % len(params))
145>>> h.putheader('Accept', 'text/plain')
146>>> h.putheader('Host', 'www.musi-cal.com')
147>>> h.endheaders()
Fred Draked99e5342000-11-17 18:04:03 +0000148>>> h.send(params)
Fred Drake4e716fa2000-06-28 21:51:43 +0000149>>> reply, msg, hdrs = h.getreply()
Fred Draked99e5342000-11-17 18:04:03 +0000150>>> print reply # should be 200
Fred Drake4e716fa2000-06-28 21:51:43 +0000151>>> data = h.getfile().read() # get the raw HTML
152\end{verbatim}