blob: d578ae2e6d9594ec2170d8cab6bc47643d93256f [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{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 Drakea2e98181998-03-12 05:54:02 +000012The module defines one class, \class{HTTP}. An \class{HTTP} instance
Guido van Rossuma12ef941995-02-27 17:53:25 +000013represents one transaction with an HTTP server. It should be
14instantiated passing it a host and optional port number. If no port
15number is passed, the port is extracted from the host string if it has
Fred Drakea2e98181998-03-12 05:54:02 +000016the form \code{\var{host}:\var{port}}, else the default HTTP port (80)
17is used. If no host is passed, no connection is made, and the
18\method{connect()} method should be used to connect to a server. For
19example, the following calls all create instances that connect to the
20server at the same host and port:
Guido van Rossum470be141995-03-17 16:07:09 +000021
Fred Drake19479911998-02-13 06:58:54 +000022\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000023>>> h1 = httplib.HTTP('www.cwi.nl')
24>>> h2 = httplib.HTTP('www.cwi.nl:80')
25>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000026\end{verbatim}
Fred Drakea2e98181998-03-12 05:54:02 +000027
28Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossuma12ef941995-02-27 17:53:25 +000029should be used as follows:
30
31\begin{enumerate}
32
Fred Drakea2e98181998-03-12 05:54:02 +000033\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000034
Fred Drakea2e98181998-03-12 05:54:02 +000035\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000036
Fred Drakea2e98181998-03-12 05:54:02 +000037\item[3.] Call the \method{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000038step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000039
Fred Drakea2e98181998-03-12 05:54:02 +000040\item[4.] Optional calls to the \method{send()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000041
Fred Drakea2e98181998-03-12 05:54:02 +000042\item[5.] Call the \method{getreply()} method.
Guido van Rossuma12ef941995-02-27 17:53:25 +000043
Fred Drakea2e98181998-03-12 05:54:02 +000044\item[6.] Call the \method{getfile()} method and read the data off the
Guido van Rossuma12ef941995-02-27 17:53:25 +000045file object that it returns.
46
47\end{enumerate}
48
Guido van Rossumecde7811995-03-28 13:35:14 +000049\subsection{HTTP Objects}
50
Fred Drakea2e98181998-03-12 05:54:02 +000051\class{HTTP} instances have the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000052
Fred Drake19479911998-02-13 06:58:54 +000053\setindexsubitem{(HTTP method)}
Guido van Rossumecde7811995-03-28 13:35:14 +000054
Guido van Rossuma12ef941995-02-27 17:53:25 +000055\begin{funcdesc}{set_debuglevel}{level}
56Set the debugging level (the amount of debugging output printed).
57The default debug level is \code{0}, meaning no debugging output is
58printed.
59\end{funcdesc}
60
Fred Drakea2e98181998-03-12 05:54:02 +000061\begin{funcdesc}{connect}{host\optional{, port}}
Guido van Rossuma12ef941995-02-27 17:53:25 +000062Connect to the server given by \var{host} and \var{port}. See the
63intro for the default port. This should be called directly only if
64the instance was instantiated without passing a host.
65\end{funcdesc}
66
67\begin{funcdesc}{send}{data}
68Send data to the server. This should be used directly only after the
Fred Drakea2e98181998-03-12 05:54:02 +000069\method{endheaders()} method has been called and before
70\method{getreply()} has been called.
Guido van Rossuma12ef941995-02-27 17:53:25 +000071\end{funcdesc}
72
Fred Drakea2e98181998-03-12 05:54:02 +000073\begin{funcdesc}{putrequest}{request, selector}
Guido van Rossuma12ef941995-02-27 17:53:25 +000074This should be the first call after the connection to the server has
75been made. It sends a line to the server consisting of the
76\var{request} string, the \var{selector} string, and the HTTP version
77(\code{HTTP/1.0}).
78\end{funcdesc}
79
Fred Drakea2e98181998-03-12 05:54:02 +000080\begin{funcdesc}{putheader}{header, argument\optional{, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000081Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000082server consisting of the header, a colon and a space, and the first
83argument. If more arguments are given, continuation lines are sent,
84each consisting of a tab and an argument.
85\end{funcdesc}
86
87\begin{funcdesc}{endheaders}{}
88Send a blank line to the server, signalling the end of the headers.
89\end{funcdesc}
90
91\begin{funcdesc}{getreply}{}
92Complete the request by shutting down the sending end of the socket,
Fred Drakea2e98181998-03-12 05:54:02 +000093read the reply from the server, and return a triple
94\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
95\var{replycode} is the integer reply code from the request (e.g.\
96\code{200} if the request was handled properly); \var{message} is the
97message string corresponding to the reply code; and \var{headers} is
98an instance of the class \class{mimetools.Message} containing the
99headers received from the server. See the description of the
100\module{mimetools}\refstmodindex{mimetools} module.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000101\end{funcdesc}
102
103\begin{funcdesc}{getfile}{}
104Return a file object from which the data returned by the server can be
Fred Drakea2e98181998-03-12 05:54:02 +0000105read, using the \method{read()}, \method{readline()} or
106\method{readlines()} methods.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000107\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000108
109\subsection{Example}
Guido van Rossum86cb0921995-03-20 12:59:56 +0000110\nodename{HTTP Example}
Guido van Rossum470be141995-03-17 16:07:09 +0000111
112Here is an example session:
113
Fred Drake19479911998-02-13 06:58:54 +0000114\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000115>>> import httplib
116>>> h = httplib.HTTP('www.cwi.nl')
117>>> h.putrequest('GET', '/index.html')
118>>> h.putheader('Accept', 'text/html')
119>>> h.putheader('Accept', 'text/plain')
120>>> h.endheaders()
121>>> errcode, errmsg, headers = h.getreply()
122>>> print errcode # Should be 200
123>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000124>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000125>>> f.close()
126>>>
Fred Drake19479911998-02-13 06:58:54 +0000127\end{verbatim}