cpython/Doc/lib/libhttplib.tex

129 lines
4.3 KiB
TeX
Raw Normal View History

\section{Standard Module \sectcode{httplib}}
\label{module-httplib}
1995-02-27 13:53:25 -04:00
\stmodindex{httplib}
\index{HTTP}
\renewcommand{\indexsubitem}{(in module httplib)}
1995-02-27 13:53:25 -04:00
This module defines a class which implements the client side of the
HTTP protocol. It is normally not used directly --- the module
1995-03-07 06:14:09 -04:00
\code{urllib} uses it to handle URLs that use HTTP.
1997-12-15 17:56:05 -04:00
\refstmodindex{urllib}
1995-02-27 13:53:25 -04:00
The module defines one class, \code{HTTP}. An \code{HTTP} instance
represents one transaction with an HTTP server. It should be
instantiated passing it a host and optional port number. If no port
number is passed, the port is extracted from the host string if it has
the form \code{host:port}, else the default HTTP port (80) is used.
If no host is passed, no connection is made, and the \code{connect}
method should be used to connect to a server. For example, the
following calls all create instances that connect to the server at the
same host and port:
\bcode\begin{verbatim}
>>> h1 = httplib.HTTP('www.cwi.nl')
>>> h2 = httplib.HTTP('www.cwi.nl:80')
>>> h3 = httplib.HTTP('www.cwi.nl', 80)
\end{verbatim}\ecode
%
1995-02-27 13:53:25 -04:00
Once an \code{HTTP} instance has been connected to an HTTP server, it
should be used as follows:
\begin{enumerate}
\item[1.] Make exactly one call to the \code{putrequest()} method.
\item[2.] Make zero or more calls to the \code{putheader()} method.
\item[3.] Call the \code{endheaders()} method (this can be omitted if
step 4 makes no calls).
1995-02-27 13:53:25 -04:00
\item[4.] Optional calls to the \code{send()} method.
\item[5.] Call the \code{getreply()} method.
\item[6.] Call the \code{getfile()} method and read the data off the
file object that it returns.
\end{enumerate}
\subsection{HTTP Objects}
1995-02-27 13:53:25 -04:00
\code{HTTP} instances have the following methods:
\renewcommand{\indexsubitem}{(HTTP method)}
1995-02-27 13:53:25 -04:00
\begin{funcdesc}{set_debuglevel}{level}
Set the debugging level (the amount of debugging output printed).
The default debug level is \code{0}, meaning no debugging output is
printed.
\end{funcdesc}
\begin{funcdesc}{connect}{host\optional{\, port}}
Connect to the server given by \var{host} and \var{port}. See the
intro for the default port. This should be called directly only if
the instance was instantiated without passing a host.
\end{funcdesc}
\begin{funcdesc}{send}{data}
Send data to the server. This should be used directly only after the
\code{endheaders()} method has been called and before
\code{getreply()} has been called.
\end{funcdesc}
\begin{funcdesc}{putrequest}{request\, selector}
This should be the first call after the connection to the server has
been made. It sends a line to the server consisting of the
\var{request} string, the \var{selector} string, and the HTTP version
(\code{HTTP/1.0}).
\end{funcdesc}
\begin{funcdesc}{putheader}{header\, argument\optional{\, ...}}
Send an \rfc{822} style header to the server. It sends a line to the
1995-02-27 13:53:25 -04:00
server consisting of the header, a colon and a space, and the first
argument. If more arguments are given, continuation lines are sent,
each consisting of a tab and an argument.
\end{funcdesc}
\begin{funcdesc}{endheaders}{}
Send a blank line to the server, signalling the end of the headers.
\end{funcdesc}
\begin{funcdesc}{getreply}{}
Complete the request by shutting down the sending end of the socket,
read the reply from the server, and return a triple (\var{replycode},
\var{message}, \var{headers}). Here \var{replycode} is the integer
1995-03-07 06:14:09 -04:00
reply code from the request (e.g.\ \code{200} if the request was
1995-02-27 13:53:25 -04:00
handled properly); \var{message} is the message string corresponding
1997-06-20 13:55:08 -03:00
to the reply code; and \var{headers} is an instance of the class
\code{mimetools.Message} containing the headers received from the server.
See the description of the \code{mimetools} module.
1997-12-15 17:56:05 -04:00
\refstmodindex{mimetools}
1995-02-27 13:53:25 -04:00
\end{funcdesc}
\begin{funcdesc}{getfile}{}
Return a file object from which the data returned by the server can be
read, using the \code{read()}, \code{readline()} or \code{readlines()}
methods.
\end{funcdesc}
\subsection{Example}
\nodename{HTTP Example}
Here is an example session:
\bcode\begin{verbatim}
>>> import httplib
>>> h = httplib.HTTP('www.cwi.nl')
>>> h.putrequest('GET', '/index.html')
>>> h.putheader('Accept', 'text/html')
>>> h.putheader('Accept', 'text/plain')
>>> h.endheaders()
>>> errcode, errmsg, headers = h.getreply()
>>> print errcode # Should be 200
>>> f = h.getfile()
>>> data = f.read() # Get the raw HTML
>>> f.close()
>>>
\end{verbatim}\ecode