blob: d2e242092b0ee09fa85d1155927a7f0ae7aa68e3 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{httplib}}
Guido van Rossuma12ef941995-02-27 17:53:25 +00002\stmodindex{httplib}
3\index{HTTP}
4
Guido van Rossum86751151995-02-28 17:14:32 +00005\renewcommand{\indexsubitem}{(in module httplib)}
6
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
Guido van Rossum6c4f0031995-03-07 10:14:09 +00009\code{urllib} uses it to handle URLs that use HTTP.
Guido van Rossuma12ef941995-02-27 17:53:25 +000010\stmodindex{urllib}
11
12The module defines one class, \code{HTTP}. An \code{HTTP} instance
13represents 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
16the form \code{host:port}, else the default HTTP port (80) is used.
17If no host is passed, no connection is made, and the \code{connect}
Guido van Rossum470be141995-03-17 16:07:09 +000018method should be used to connect to a server. For example, the
19following calls all create instances that connect to the server at the
20same host and port:
21
22\begin{verbatim}
23>>> h1 = httplib.HTTP('www.cwi.nl')
24>>> h2 = httplib.HTTP('www.cwi.nl:80')
25>>> h3 = httplib.HTTP('www.cwi.nl', 80)
26\end{verbatim}
Guido van Rossuma12ef941995-02-27 17:53:25 +000027
28Once an \code{HTTP} instance has been connected to an HTTP server, it
29should be used as follows:
30
31\begin{enumerate}
32
33\item[1.] Make exactly one call to the \code{putrequest()} method.
34
35\item[2.] Make zero or more calls to the \code{putheader()} method.
36
37\item[3.] Call the \code{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
40\item[4.] Optional calls to the \code{send()} method.
41
42\item[5.] Call the \code{getreply()} method.
43
44\item[6.] Call the \code{getfile()} method and read the data off the
45file object that it returns.
46
47\end{enumerate}
48
Guido van Rossumecde7811995-03-28 13:35:14 +000049\subsection{HTTP Objects}
50
Guido van Rossuma12ef941995-02-27 17:53:25 +000051\code{HTTP} instances have the following methods:
52
Guido van Rossumecde7811995-03-28 13:35:14 +000053\renewcommand{\indexsubitem}{(HTTP method)}
54
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
61\begin{funcdesc}{connect}{host\optional{\, port}}
62Connect 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
69\code{endheaders()} method has been called and before
70\code{getreply()} has been called.
71\end{funcdesc}
72
73\begin{funcdesc}{putrequest}{request\, selector}
74This 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
80\begin{funcdesc}{putheader}{header\, argument\optional{\, ...}}
81Send an RFC-822 style header to the server. It sends a line to the
82server 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,
93read the reply from the server, and return a triple (\var{replycode},
94\var{message}, \var{headers}). Here \var{replycode} is the integer
Guido van Rossum6c4f0031995-03-07 10:14:09 +000095reply code from the request (e.g.\ \code{200} if the request was
Guido van Rossuma12ef941995-02-27 17:53:25 +000096handled properly); \var{message} is the message string corresponding
97to the reply code; and \var{header} is an instance of the class
98\code{rfc822.Message} containing the headers received from the server.
99See the description of the \code{rfc822} module.
100\stmodindex{rfc822}
101\end{funcdesc}
102
103\begin{funcdesc}{getfile}{}
104Return a file object from which the data returned by the server can be
105read, using the \code{read()}, \code{readline()} or \code{readlines()}
106methods.
107\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
114\begin{verbatim}
115>>> 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()
124>>> data f.read() # Get the raw HTML
125>>> f.close()
126>>>
127\end{verbatim}