1998-08-10 16:42:37 -03:00
|
|
|
\section{\module{httplib} ---
|
1999-04-22 13:47:27 -03:00
|
|
|
HTTP protocol client}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
1999-04-22 13:47:27 -03:00
|
|
|
\declaremodule{standard}{httplib}
|
1998-07-23 14:59:49 -03:00
|
|
|
\modulesynopsis{HTTP protocol client (requires sockets).}
|
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\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
|
1999-04-22 13:47:27 -03:00
|
|
|
\refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs that
|
|
|
|
use HTTP.
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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
|
1998-03-12 01:54:02 -04:00
|
|
|
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:
|
1995-03-17 12:07:09 -04:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-03-17 12:07:09 -04:00
|
|
|
>>> h1 = httplib.HTTP('www.cwi.nl')
|
|
|
|
>>> h2 = httplib.HTTP('www.cwi.nl:80')
|
|
|
|
>>> h3 = httplib.HTTP('www.cwi.nl', 80)
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1998-03-12 01:54:02 -04:00
|
|
|
|
|
|
|
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}
|
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\item[1.] Make exactly one call to the \method{putrequest()} method.
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\item[2.] Make zero or more calls to the \method{putheader()} method.
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\item[3.] Call the \method{endheaders()} method (this can be omitted if
|
1995-03-17 12:07:09 -04:00
|
|
|
step 4 makes no calls).
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\item[4.] Optional calls to the \method{send()} method.
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\item[5.] Call the \method{getreply()} method.
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-03-12 01:54:02 -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}
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{classdesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1995-03-28 09:35:14 -04:00
|
|
|
\subsection{HTTP Objects}
|
|
|
|
|
1998-03-12 01:54:02 -04:00
|
|
|
\class{HTTP} instances have the following methods:
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1995-03-28 09:35:14 -04:00
|
|
|
|
1998-04-04 03:15:02 -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.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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
|
1998-03-12 01:54:02 -04:00
|
|
|
\method{endheaders()} method has been called and before
|
|
|
|
\method{getreply()} has been called.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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}).
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -04:00
|
|
|
\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
|
1998-02-09 15:16:20 -04:00
|
|
|
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.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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,
|
1998-03-12 01:54:02 -04:00
|
|
|
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.,
|
1998-03-12 01:54:02 -04:00
|
|
|
\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
|
1999-04-22 13:47:27 -03:00
|
|
|
\refmodule{mimetools}\refstmodindex{mimetools} module.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-02-27 13:53:25 -04:00
|
|
|
|
1998-04-04 03:15:02 -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
|
1998-03-12 01:54:02 -04:00
|
|
|
read, using the \method{read()}, \method{readline()} or
|
|
|
|
\method{readlines()} methods.
|
1998-04-04 03:15:02 -04:00
|
|
|
\end{methoddesc}
|
1995-03-17 12:07:09 -04:00
|
|
|
|
2000-06-28 19:09:20 -03:00
|
|
|
\subsection{Examples}
|
|
|
|
\nodename{HTTP Examples}
|
1995-03-17 12:07:09 -04:00
|
|
|
|
2000-06-28 18:51:43 -03:00
|
|
|
Here is an example session that uses the \samp{GET} method:
|
1995-03-17 12:07:09 -04:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1995-03-17 12:07:09 -04:00
|
|
|
>>> 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()
|
1997-12-02 16:08:06 -04:00
|
|
|
>>> data = f.read() # Get the raw HTML
|
1995-03-17 12:07:09 -04:00
|
|
|
>>> f.close()
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
2000-06-28 18:51:43 -03:00
|
|
|
|
|
|
|
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")
|
2000-11-17 14:04:03 -04:00
|
|
|
>>> h.putheader("Content-type", "application/x-www-form-urlencoded")
|
2000-06-28 18:51:43 -03:00
|
|
|
>>> h.putheader("Content-length", "%d" % len(params))
|
|
|
|
>>> h.putheader('Accept', 'text/plain')
|
|
|
|
>>> h.putheader('Host', 'www.musi-cal.com')
|
|
|
|
>>> h.endheaders()
|
2000-11-17 14:04:03 -04:00
|
|
|
>>> h.send(params)
|
2000-06-28 18:51:43 -03:00
|
|
|
>>> reply, msg, hdrs = h.getreply()
|
2000-11-17 14:04:03 -04:00
|
|
|
>>> print reply # should be 200
|
2000-06-28 18:51:43 -03:00
|
|
|
>>> data = h.getfile().read() # get the raw HTML
|
|
|
|
\end{verbatim}
|