blob: efdeac3f95ce38447213080042ed174682ac666a [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 Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{HTTP protocol client (requires sockets).}
6
Fred Drakea2e98181998-03-12 05:54:02 +00007\indexii{HTTP}{protocol}
Guido van Rossuma12ef941995-02-27 17:53:25 +00008
Guido van Rossuma12ef941995-02-27 17:53:25 +00009This module defines a class which implements the client side of the
10HTTP protocol. It is normally not used directly --- the module
Fred Drake12a95691999-04-22 16:47:27 +000011\refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs that
12use HTTP.
Guido van Rossuma12ef941995-02-27 17:53:25 +000013
Fred Drakefc576191998-04-04 07:15:02 +000014The module defines one class, \class{HTTP}:
15
16\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
17An \class{HTTP} instance
Guido van Rossuma12ef941995-02-27 17:53:25 +000018represents one transaction with an HTTP server. It should be
19instantiated passing it a host and optional port number. If no port
20number is passed, the port is extracted from the host string if it has
Fred Drakea2e98181998-03-12 05:54:02 +000021the form \code{\var{host}:\var{port}}, else the default HTTP port (80)
22is used. If no host is passed, no connection is made, and the
23\method{connect()} method should be used to connect to a server. For
24example, the following calls all create instances that connect to the
25server at the same host and port:
Guido van Rossum470be141995-03-17 16:07:09 +000026
Fred Drake19479911998-02-13 06:58:54 +000027\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000028>>> h1 = httplib.HTTP('www.cwi.nl')
29>>> h2 = httplib.HTTP('www.cwi.nl:80')
30>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000031\end{verbatim}
Fred Drakea2e98181998-03-12 05:54:02 +000032
33Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossuma12ef941995-02-27 17:53:25 +000034should be used as follows:
35
36\begin{enumerate}
37
Fred Drakea2e98181998-03-12 05:54:02 +000038\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000039
Fred Drakea2e98181998-03-12 05:54:02 +000040\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000041
Fred Drakea2e98181998-03-12 05:54:02 +000042\item[3.] Call the \method{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000043step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000044
Fred Drakea2e98181998-03-12 05:54:02 +000045\item[4.] Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000046
Fred Drakea2e98181998-03-12 05:54:02 +000047\item[5.] Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000048
Fred Drakea2e98181998-03-12 05:54:02 +000049\item[6.] Call the \method{getfile()} method and read the data off the
Guido van Rossuma12ef941995-02-27 17:53:25 +000050file object that it returns.
51
52\end{enumerate}
Fred Drakefc576191998-04-04 07:15:02 +000053\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000054
Guido van Rossumecde7811995-03-28 13:35:14 +000055\subsection{HTTP Objects}
56
Fred Drakea2e98181998-03-12 05:54:02 +000057\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000058
Guido van Rossumecde7811995-03-28 13:35:14 +000059
Fred Drakefc576191998-04-04 07:15:02 +000060\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +000061Set the debugging level (the amount of debugging output printed).
62The default debug level is \code{0}, meaning no debugging output is
63printed.
Fred Drakefc576191998-04-04 07:15:02 +000064\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000065
Fred Drakefc576191998-04-04 07:15:02 +000066\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000067Connect to the server given by \var{host} and \var{port}. See the
68intro for the default port. This should be called directly only if
69the instance was instantiated without passing a host.
Fred Drakefc576191998-04-04 07:15:02 +000070\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000071
Fred Drakefc576191998-04-04 07:15:02 +000072\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +000073Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000074\method{endheaders()} method has been called and before
75\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +000076\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000077
Fred Drakefc576191998-04-04 07:15:02 +000078\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000079This should be the first call after the connection to the server has
80been made. It sends a line to the server consisting of the
81\var{request} string, the \var{selector} string, and the HTTP version
82(\code{HTTP/1.0}).
Fred Drakefc576191998-04-04 07:15:02 +000083\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000084
Fred Drakefc576191998-04-04 07:15:02 +000085\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000086Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000087server consisting of the header, a colon and a space, and the first
88argument. If more arguments are given, continuation lines are sent,
89each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +000090\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000091
Fred Drakefc576191998-04-04 07:15:02 +000092\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000093Send a blank line to the server, signalling the end of the headers.
Fred Drakefc576191998-04-04 07:15:02 +000094\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}{getreply}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000097Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +000098read the reply from the server, and return a triple
99\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
Fred Drake17765561998-11-30 19:00:16 +0000100\var{replycode} is the integer reply code from the request (e.g.,
Fred Drakea2e98181998-03-12 05:54:02 +0000101\code{200} if the request was handled properly); \var{message} is the
102message string corresponding to the reply code; and \var{headers} is
103an instance of the class \class{mimetools.Message} containing the
104headers received from the server. See the description of the
Fred Drake12a95691999-04-22 16:47:27 +0000105\refmodule{mimetools}\refstmodindex{mimetools} module.
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}{getfile}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000109Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000110read, using the \method{read()}, \method{readline()} or
111\method{readlines()} methods.
Fred Drakefc576191998-04-04 07:15:02 +0000112\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000113
114\subsection{Example}
Guido van Rossum86cb0921995-03-20 12:59:56 +0000115\nodename{HTTP Example}
Guido van Rossum470be141995-03-17 16:07:09 +0000116
117Here is an example session:
118
Fred Drake19479911998-02-13 06:58:54 +0000119\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000120>>> import httplib
121>>> h = httplib.HTTP('www.cwi.nl')
122>>> h.putrequest('GET', '/index.html')
123>>> h.putheader('Accept', 'text/html')
124>>> h.putheader('Accept', 'text/plain')
125>>> h.endheaders()
126>>> errcode, errmsg, headers = h.getreply()
127>>> print errcode # Should be 200
128>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000129>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000130>>> f.close()
Fred Drake19479911998-02-13 06:58:54 +0000131\end{verbatim}