blob: ed59c9cfd3121f3a9e802b0d05f0659914e9facb [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}
4\index{HTTP}
5
Fred Drake19479911998-02-13 06:58:54 +00006\setindexsubitem{(in module httplib)}
Guido van Rossum86751151995-02-28 17:14:32 +00007
Guido van Rossuma12ef941995-02-27 17:53:25 +00008This module defines a class which implements the client side of the
9HTTP protocol. It is normally not used directly --- the module
Guido van Rossum6c4f0031995-03-07 10:14:09 +000010\code{urllib} uses it to handle URLs that use HTTP.
Fred Drake54820dc1997-12-15 21:56:05 +000011\refstmodindex{urllib}
Guido van Rossuma12ef941995-02-27 17:53:25 +000012
13The module defines one class, \code{HTTP}. An \code{HTTP} instance
14represents one transaction with an HTTP server. It should be
15instantiated passing it a host and optional port number. If no port
16number is passed, the port is extracted from the host string if it has
17the form \code{host:port}, else the default HTTP port (80) is used.
18If no host is passed, no connection is made, and the \code{connect}
Guido van Rossum470be141995-03-17 16:07:09 +000019method should be used to connect to a server. For example, the
20following calls all create instances that connect to the server at the
21same host and port:
22
Fred Drake19479911998-02-13 06:58:54 +000023\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +000024>>> h1 = httplib.HTTP('www.cwi.nl')
25>>> h2 = httplib.HTTP('www.cwi.nl:80')
26>>> h3 = httplib.HTTP('www.cwi.nl', 80)
Fred Drake19479911998-02-13 06:58:54 +000027\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000028%
Guido van Rossuma12ef941995-02-27 17:53:25 +000029Once an \code{HTTP} instance has been connected to an HTTP server, it
30should be used as follows:
31
32\begin{enumerate}
33
34\item[1.] Make exactly one call to the \code{putrequest()} method.
35
36\item[2.] Make zero or more calls to the \code{putheader()} method.
37
38\item[3.] Call the \code{endheaders()} method (this can be omitted if
Guido van Rossum470be141995-03-17 16:07:09 +000039step 4 makes no calls).
Guido van Rossuma12ef941995-02-27 17:53:25 +000040
41\item[4.] Optional calls to the \code{send()} method.
42
43\item[5.] Call the \code{getreply()} method.
44
45\item[6.] Call the \code{getfile()} method and read the data off the
46file object that it returns.
47
48\end{enumerate}
49
Guido van Rossumecde7811995-03-28 13:35:14 +000050\subsection{HTTP Objects}
51
Guido van Rossuma12ef941995-02-27 17:53:25 +000052\code{HTTP} instances have the following methods:
53
Fred Drake19479911998-02-13 06:58:54 +000054\setindexsubitem{(HTTP method)}
Guido van Rossumecde7811995-03-28 13:35:14 +000055
Guido van Rossuma12ef941995-02-27 17:53:25 +000056\begin{funcdesc}{set_debuglevel}{level}
57Set the debugging level (the amount of debugging output printed).
58The default debug level is \code{0}, meaning no debugging output is
59printed.
60\end{funcdesc}
61
62\begin{funcdesc}{connect}{host\optional{\, port}}
63Connect to the server given by \var{host} and \var{port}. See the
64intro for the default port. This should be called directly only if
65the instance was instantiated without passing a host.
66\end{funcdesc}
67
68\begin{funcdesc}{send}{data}
69Send data to the server. This should be used directly only after the
70\code{endheaders()} method has been called and before
71\code{getreply()} has been called.
72\end{funcdesc}
73
74\begin{funcdesc}{putrequest}{request\, selector}
75This should be the first call after the connection to the server has
76been made. It sends a line to the server consisting of the
77\var{request} string, the \var{selector} string, and the HTTP version
78(\code{HTTP/1.0}).
79\end{funcdesc}
80
81\begin{funcdesc}{putheader}{header\, argument\optional{\, ...}}
Fred Drakec5891241998-02-09 19:16:20 +000082Send an \rfc{822} style header to the server. It sends a line to the
Guido van Rossuma12ef941995-02-27 17:53:25 +000083server consisting of the header, a colon and a space, and the first
84argument. If more arguments are given, continuation lines are sent,
85each consisting of a tab and an argument.
86\end{funcdesc}
87
88\begin{funcdesc}{endheaders}{}
89Send a blank line to the server, signalling the end of the headers.
90\end{funcdesc}
91
92\begin{funcdesc}{getreply}{}
93Complete the request by shutting down the sending end of the socket,
94read the reply from the server, and return a triple (\var{replycode},
95\var{message}, \var{headers}). Here \var{replycode} is the integer
Guido van Rossum6c4f0031995-03-07 10:14:09 +000096reply code from the request (e.g.\ \code{200} if the request was
Guido van Rossuma12ef941995-02-27 17:53:25 +000097handled properly); \var{message} is the message string corresponding
Fred Drakedd527331997-06-20 16:55:08 +000098to the reply code; and \var{headers} is an instance of the class
Guido van Rossuma3f53cd1997-06-02 17:26:30 +000099\code{mimetools.Message} containing the headers received from the server.
100See the description of the \code{mimetools} module.
Fred Drake54820dc1997-12-15 21:56:05 +0000101\refstmodindex{mimetools}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000102\end{funcdesc}
103
104\begin{funcdesc}{getfile}{}
105Return a file object from which the data returned by the server can be
106read, using the \code{read()}, \code{readline()} or \code{readlines()}
107methods.
108\end{funcdesc}
Guido van Rossum470be141995-03-17 16:07:09 +0000109
110\subsection{Example}
Guido van Rossum86cb0921995-03-20 12:59:56 +0000111\nodename{HTTP Example}
Guido van Rossum470be141995-03-17 16:07:09 +0000112
113Here is an example session:
114
Fred Drake19479911998-02-13 06:58:54 +0000115\begin{verbatim}
Guido van Rossum470be141995-03-17 16:07:09 +0000116>>> import httplib
117>>> h = httplib.HTTP('www.cwi.nl')
118>>> h.putrequest('GET', '/index.html')
119>>> h.putheader('Accept', 'text/html')
120>>> h.putheader('Accept', 'text/plain')
121>>> h.endheaders()
122>>> errcode, errmsg, headers = h.getreply()
123>>> print errcode # Should be 200
124>>> f = h.getfile()
Guido van Rossum240ddc81997-12-02 20:08:06 +0000125>>> data = f.read() # Get the raw HTML
Guido van Rossum470be141995-03-17 16:07:09 +0000126>>> f.close()
127>>>
Fred Drake19479911998-02-13 06:58:54 +0000128\end{verbatim}