cpython/Doc/lib/libhttplib.tex

149 lines
5.0 KiB
TeX
Raw Normal View History

\section{\module{httplib} ---
HTTP protocol client}
\declaremodule{standard}{httplib}
\modulesynopsis{HTTP protocol client (requires sockets).}
\indexii{HTTP}{protocol}
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
\refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs that
use HTTP.
1995-02-27 13:53:25 -04:00
The module defines one class, \class{HTTP}:
\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
An \class{HTTP} instance
1995-02-27 13:53:25 -04:00
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{\var{host}:\var{port}}, else the default HTTP port (80)
is used. If no host is passed, no connection is made, and the
\method{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:
\begin{verbatim}
>>> h1 = httplib.HTTP('www.cwi.nl')
>>> h2 = httplib.HTTP('www.cwi.nl:80')
>>> h3 = httplib.HTTP('www.cwi.nl', 80)
\end{verbatim}
Once an \class{HTTP} instance has been connected to an HTTP server, it
1995-02-27 13:53:25 -04:00
should be used as follows:
\begin{enumerate}
\item[1.] Make exactly one call to the \method{putrequest()} method.
1995-02-27 13:53:25 -04:00
\item[2.] Make zero or more calls to the \method{putheader()} method.
1995-02-27 13:53:25 -04:00
\item[3.] Call the \method{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 \method{send()} method.
1995-02-27 13:53:25 -04:00
\item[5.] Call the \method{getreply()} method.
1995-02-27 13:53:25 -04:00
\item[6.] Call the \method{getfile()} method and read the data off the
1995-02-27 13:53:25 -04:00
file object that it returns.
\end{enumerate}
\end{classdesc}
1995-02-27 13:53:25 -04:00
\subsection{HTTP Objects}
\class{HTTP} instances have the following methods:
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{set_debuglevel}{level}
1995-02-27 13:53:25 -04:00
Set the debugging level (the amount of debugging output printed).
The default debug level is \code{0}, meaning no debugging output is
printed.
\end{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{connect}{host\optional{, port}}
1995-02-27 13:53:25 -04:00
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{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{send}{data}
1995-02-27 13:53:25 -04:00
Send data to the server. This should be used directly only after the
\method{endheaders()} method has been called and before
\method{getreply()} has been called.
\end{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{putrequest}{request, selector}
1995-02-27 13:53:25 -04:00
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{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{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{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{endheaders}{}
1995-02-27 13:53:25 -04:00
Send a blank line to the server, signalling the end of the headers.
\end{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{getreply}{}
1995-02-27 13:53:25 -04:00
Complete the request by shutting down the sending end of the socket,
read the reply from the server, and return a triple
\code{(\var{replycode}, \var{message}, \var{headers})}. Here,
1998-11-30 15:00:16 -04:00
\var{replycode} is the integer reply code from the request (e.g.,
\code{200} if the request was handled properly); \var{message} is the
message string corresponding to the reply code; and \var{headers} is
an instance of the class \class{mimetools.Message} containing the
headers received from the server. See the description of the
\refmodule{mimetools}\refstmodindex{mimetools} module.
\end{methoddesc}
1995-02-27 13:53:25 -04:00
\begin{methoddesc}{getfile}{}
1995-02-27 13:53:25 -04:00
Return a file object from which the data returned by the server can be
read, using the \method{read()}, \method{readline()} or
\method{readlines()} methods.
\end{methoddesc}
\subsection{Examples}
\nodename{HTTP Examples}
Here is an example session that uses the \samp{GET} method:
\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}
Here is an example session that shows how to \samp{POST} requests:
\begin{verbatim}
>>> import httplib, urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> h = httplib.HTTP("www.musi-cal.com:80")
>>> h.putrequest("POST", "/cgi-bin/query")
>>> h.putheader("Content-length", "%d" % len(params))
>>> h.putheader('Accept', 'text/plain')
>>> h.putheader('Host', 'www.musi-cal.com')
>>> h.endheaders()
>>> h.send(paramstring)
>>> reply, msg, hdrs = h.getreply()
>>> print errcode # should be 200
>>> data = h.getfile().read() # get the raw HTML
\end{verbatim}