blob: 594a15564a8062529265c92b4fd33bebd84a98f0 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{httplib} ---
2 HTTP protocol client.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{httplib}
4
5\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 Rossum86751151995-02-28 17:14:32 +00009
Guido van Rossuma12ef941995-02-27 17:53:25 +000010This module defines a class which implements the client side of the
11HTTP protocol. It is normally not used directly --- the module
Fred Drakea2e98181998-03-12 05:54:02 +000012\module{urllib}\refstmodindex{urllib} uses it to handle URLs that use
13HTTP.
Guido van Rossuma12ef941995-02-27 17:53:25 +000014
Fred Drakefc576191998-04-04 07:15:02 +000015The module defines one class, \class{HTTP}:
16
17\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
18An \class{HTTP} instance
Guido van Rossuma12ef941995-02-27 17:53:25 +000019represents one transaction with an HTTP server. It should be
20instantiated passing it a host and optional port number. If no port
21number is passed, the port is extracted from the host string if it has
Fred Drakea2e98181998-03-12 05:54:02 +000022the form \code{\var{host}:\var{port}}, else the default HTTP port (80)
23is used. If no host is passed, no connection is made, and the
24\method{connect()} method should be used to connect to a server. For
25example, the following calls all create instances that connect to the
26server at the same host and port:
Guido van Rossum470be141995-03-17 16:07:09 +000027
Fred Drake19479911998-02-13 06:58:54 +000028\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000029>>> h1 = httplib.HTTP('www.cwi.nl')
30>>> h2 = httplib.HTTP('www.cwi.nl:80')
31>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000032\end{verbatim}
Fred Drakea2e98181998-03-12 05:54:02 +000033
34Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossuma12ef941995-02-27 17:53:25 +000035should be used as follows:
36
37\begin{enumerate}
38
Fred Drakea2e98181998-03-12 05:54:02 +000039\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000040
Fred Drakea2e98181998-03-12 05:54:02 +000041\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000042
Fred Drakea2e98181998-03-12 05:54:02 +000043\item[3.] Call the \method{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000044step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000045
Fred Drakea2e98181998-03-12 05:54:02 +000046\item[4.] Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000047
Fred Drakea2e98181998-03-12 05:54:02 +000048\item[5.] Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000049
Fred Drakea2e98181998-03-12 05:54:02 +000050\item[6.] Call the \method{getfile()} method and read the data off the
Guido van Rossuma12ef941995-02-27 17:53:25 +000051file object that it returns.
52
53\end{enumerate}
Fred Drakefc576191998-04-04 07:15:02 +000054\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000055
Guido van Rossumecde7811995-03-28 13:35:14 +000056\subsection{HTTP Objects}
57
Fred Drakea2e98181998-03-12 05:54:02 +000058\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000059
Guido van Rossumecde7811995-03-28 13:35:14 +000060
Fred Drakefc576191998-04-04 07:15:02 +000061\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +000062Set the debugging level (the amount of debugging output printed).
63The default debug level is \code{0}, meaning no debugging output is
64printed.
Fred Drakefc576191998-04-04 07:15:02 +000065\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000066
Fred Drakefc576191998-04-04 07:15:02 +000067\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000068Connect to the server given by \var{host} and \var{port}. See the
69intro for the default port. This should be called directly only if
70the instance was instantiated without passing a host.
Fred Drakefc576191998-04-04 07:15:02 +000071\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000072
Fred Drakefc576191998-04-04 07:15:02 +000073\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +000074Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000075\method{endheaders()} method has been called and before
76\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +000077\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000078
Fred Drakefc576191998-04-04 07:15:02 +000079\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000080This should be the first call after the connection to the server has
81been made. It sends a line to the server consisting of the
82\var{request} string, the \var{selector} string, and the HTTP version
83(\code{HTTP/1.0}).
Fred Drakefc576191998-04-04 07:15:02 +000084\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000085
Fred Drakefc576191998-04-04 07:15:02 +000086\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000087Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000088server consisting of the header, a colon and a space, and the first
89argument. If more arguments are given, continuation lines are sent,
90each consisting of a tab and an argument.
Fred Drakefc576191998-04-04 07:15:02 +000091\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000092
Fred Drakefc576191998-04-04 07:15:02 +000093\begin{methoddesc}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000094Send a blank line to the server, signalling the end of the headers.
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}{getreply}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000098Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +000099read the reply from the server, and return a triple
100\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
Fred Drake17765561998-11-30 19:00:16 +0000101\var{replycode} is the integer reply code from the request (e.g.,
Fred Drakea2e98181998-03-12 05:54:02 +0000102\code{200} if the request was handled properly); \var{message} is the
103message string corresponding to the reply code; and \var{headers} is
104an instance of the class \class{mimetools.Message} containing the
105headers received from the server. See the description of the
106\module{mimetools}\refstmodindex{mimetools} module.
Fred Drakefc576191998-04-04 07:15:02 +0000107\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000108
Fred Drakefc576191998-04-04 07:15:02 +0000109\begin{methoddesc}{getfile}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000110Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000111read, using the \method{read()}, \method{readline()} or
112\method{readlines()} methods.
Fred Drakefc576191998-04-04 07:15:02 +0000113\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000114
115\subsection{Example}
Guido van Rossum86cb0921995-03-20 12:59:56 +0000116\nodename{HTTP Example}
Guido van Rossum470be141995-03-17 16:07:09 +0000117
118Here is an example session:
119
Fred Drake19479911998-02-13 06:58:54 +0000120\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000121>>> import httplib
122>>> h = httplib.HTTP('www.cwi.nl')
123>>> h.putrequest('GET', '/index.html')
124>>> h.putheader('Accept', 'text/html')
125>>> h.putheader('Accept', 'text/plain')
126>>> h.endheaders()
127>>> errcode, errmsg, headers = h.getreply()
128>>> print errcode # Should be 200
129>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000130>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000131>>> f.close()
Fred Drake19479911998-02-13 06:58:54 +0000132\end{verbatim}