blob: 55ec9b9899847219d418f8d32ec2903929e161ea [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{httplib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-httplib}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{httplib}
Fred Drakea2e98181998-03-12 05:54:02 +00004\indexii{HTTP}{protocol}
Guido van Rossuma12ef941995-02-27 17:53:25 +00005
Guido van Rossum86751151995-02-28 17:14:32 +00006
Guido van Rossuma12ef941995-02-27 17:53:25 +00007This module defines a class which implements the client side of the
8HTTP protocol. It is normally not used directly --- the module
Fred Drakea2e98181998-03-12 05:54:02 +00009\module{urllib}\refstmodindex{urllib} uses it to handle URLs that use
10HTTP.
Guido van Rossuma12ef941995-02-27 17:53:25 +000011
Fred Drakefc576191998-04-04 07:15:02 +000012The module defines one class, \class{HTTP}:
13
14\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
15An \class{HTTP} instance
Guido van Rossuma12ef941995-02-27 17:53:25 +000016represents one transaction with an HTTP server. It should be
17instantiated passing it a host and optional port number. If no port
18number is passed, the port is extracted from the host string if it has
Fred Drakea2e98181998-03-12 05:54:02 +000019the form \code{\var{host}:\var{port}}, else the default HTTP port (80)
20is used. If no host is passed, no connection is made, and the
21\method{connect()} method should be used to connect to a server. For
22example, the following calls all create instances that connect to the
23server at the same host and port:
Guido van Rossum470be141995-03-17 16:07:09 +000024
Fred Drake19479911998-02-13 06:58:54 +000025\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000026>>> h1 = httplib.HTTP('www.cwi.nl')
27>>> h2 = httplib.HTTP('www.cwi.nl:80')
28>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000029\end{verbatim}
Fred Drakea2e98181998-03-12 05:54:02 +000030
31Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossuma12ef941995-02-27 17:53:25 +000032should be used as follows:
33
34\begin{enumerate}
35
Fred Drakea2e98181998-03-12 05:54:02 +000036\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000037
Fred Drakea2e98181998-03-12 05:54:02 +000038\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000039
Fred Drakea2e98181998-03-12 05:54:02 +000040\item[3.] Call the \method{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000041step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000042
Fred Drakea2e98181998-03-12 05:54:02 +000043\item[4.] Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000044
Fred Drakea2e98181998-03-12 05:54:02 +000045\item[5.] Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000046
Fred Drakea2e98181998-03-12 05:54:02 +000047\item[6.] Call the \method{getfile()} method and read the data off the
Guido van Rossuma12ef941995-02-27 17:53:25 +000048file object that it returns.
49
50\end{enumerate}
Fred Drakefc576191998-04-04 07:15:02 +000051\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000052
Guido van Rossumecde7811995-03-28 13:35:14 +000053\subsection{HTTP Objects}
54
Fred Drakea2e98181998-03-12 05:54:02 +000055\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000056
Guido van Rossumecde7811995-03-28 13:35:14 +000057
Fred Drakefc576191998-04-04 07:15:02 +000058\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossuma12ef941995-02-27 17:53:25 +000059Set the debugging level (the amount of debugging output printed).
60The default debug level is \code{0}, meaning no debugging output is
61printed.
Fred Drakefc576191998-04-04 07:15:02 +000062\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000063
Fred Drakefc576191998-04-04 07:15:02 +000064\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000065Connect to the server given by \var{host} and \var{port}. See the
66intro for the default port. This should be called directly only if
67the instance was instantiated without passing a host.
Fred Drakefc576191998-04-04 07:15:02 +000068\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000069
Fred Drakefc576191998-04-04 07:15:02 +000070\begin{methoddesc}{send}{data}
Guido van Rossuma12ef941995-02-27 17:53:25 +000071Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000072\method{endheaders()} method has been called and before
73\method{getreply()} has been called.
Fred Drakefc576191998-04-04 07:15:02 +000074\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000075
Fred Drakefc576191998-04-04 07:15:02 +000076\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000077This should be the first call after the connection to the server has
78been made. It sends a line to the server consisting of the
79\var{request} string, the \var{selector} string, and the HTTP version
80(\code{HTTP/1.0}).
Fred Drakefc576191998-04-04 07:15:02 +000081\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000082
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000084Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000085server consisting of the header, a colon and a space, and the first
86argument. If more arguments are given, continuation lines are sent,
87each consisting of a tab and an argument.
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}{endheaders}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000091Send a blank line to the server, signalling the end of the headers.
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}{getreply}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +000095Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +000096read the reply from the server, and return a triple
97\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
98\var{replycode} is the integer reply code from the request (e.g.\
99\code{200} if the request was handled properly); \var{message} is the
100message string corresponding to the reply code; and \var{headers} is
101an instance of the class \class{mimetools.Message} containing the
102headers received from the server. See the description of the
103\module{mimetools}\refstmodindex{mimetools} module.
Fred Drakefc576191998-04-04 07:15:02 +0000104\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000105
Fred Drakefc576191998-04-04 07:15:02 +0000106\begin{methoddesc}{getfile}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000107Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000108read, using the \method{read()}, \method{readline()} or
109\method{readlines()} methods.
Fred Drakefc576191998-04-04 07:15:02 +0000110\end{methoddesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000111
112\subsection{Example}
Guido van Rossum86cb0921995-03-20 12:59:56 +0000113\nodename{HTTP Example}
Guido van Rossum470be141995-03-17 16:07:09 +0000114
115Here is an example session:
116
Fred Drake19479911998-02-13 06:58:54 +0000117\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000118>>> import httplib
119>>> h = httplib.HTTP('www.cwi.nl')
120>>> h.putrequest('GET', '/index.html')
121>>> h.putheader('Accept', 'text/html')
122>>> h.putheader('Accept', 'text/plain')
123>>> h.endheaders()
124>>> errcode, errmsg, headers = h.getreply()
125>>> print errcode # Should be 200
126>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000127>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000128>>> f.close()
Fred Drake19479911998-02-13 06:58:54 +0000129\end{verbatim}