libhttplib.tex 4.43 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1 2
\section{\module{httplib} ---
         HTTP protocol client.}
3 4 5 6
\declaremodule{standard}{httplib}

\modulesynopsis{HTTP protocol client (requires sockets).}

Fred Drake's avatar
Fred Drake committed
7
\indexii{HTTP}{protocol}
Guido van Rossum's avatar
Guido van Rossum committed
8

9

Guido van Rossum's avatar
Guido van Rossum committed
10 11
This module defines a class which implements the client side of the
HTTP protocol.  It is normally not used directly --- the module
Fred Drake's avatar
Fred Drake committed
12 13
\module{urllib}\refstmodindex{urllib} uses it to handle URLs that use
HTTP.
Guido van Rossum's avatar
Guido van Rossum committed
14

15 16 17 18
The module defines one class, \class{HTTP}:

\begin{classdesc}{HTTP}{\optional{host\optional{, port}}}
An \class{HTTP} instance
Guido van Rossum's avatar
Guido van Rossum committed
19 20 21
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
Fred Drake's avatar
Fred Drake committed
22 23 24 25 26
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:
27

28
\begin{verbatim}
29 30 31
>>> h1 = httplib.HTTP('www.cwi.nl')
>>> h2 = httplib.HTTP('www.cwi.nl:80')
>>> h3 = httplib.HTTP('www.cwi.nl', 80)
32
\end{verbatim}
Fred Drake's avatar
Fred Drake committed
33 34

Once an \class{HTTP} instance has been connected to an HTTP server, it
Guido van Rossum's avatar
Guido van Rossum committed
35 36 37 38
should be used as follows:

\begin{enumerate}

Fred Drake's avatar
Fred Drake committed
39
\item[1.] Make exactly one call to the \method{putrequest()} method.
Guido van Rossum's avatar
Guido van Rossum committed
40

Fred Drake's avatar
Fred Drake committed
41
\item[2.] Make zero or more calls to the \method{putheader()} method.
Guido van Rossum's avatar
Guido van Rossum committed
42

Fred Drake's avatar
Fred Drake committed
43
\item[3.] Call the \method{endheaders()} method (this can be omitted if
44
step 4 makes no calls).
Guido van Rossum's avatar
Guido van Rossum committed
45

Fred Drake's avatar
Fred Drake committed
46
\item[4.] Optional calls to the \method{send()} method.
Guido van Rossum's avatar
Guido van Rossum committed
47

Fred Drake's avatar
Fred Drake committed
48
\item[5.] Call the \method{getreply()} method.
Guido van Rossum's avatar
Guido van Rossum committed
49

Fred Drake's avatar
Fred Drake committed
50
\item[6.] Call the \method{getfile()} method and read the data off the
Guido van Rossum's avatar
Guido van Rossum committed
51 52 53
file object that it returns.

\end{enumerate}
54
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
55

56 57
\subsection{HTTP Objects}

Fred Drake's avatar
Fred Drake committed
58
\class{HTTP} instances have the following methods:
Guido van Rossum's avatar
Guido van Rossum committed
59

60

61
\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum's avatar
Guido van Rossum committed
62 63 64
Set the debugging level (the amount of debugging output printed).
The default debug level is \code{0}, meaning no debugging output is
printed.
65
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
66

67
\begin{methoddesc}{connect}{host\optional{, port}}
Guido van Rossum's avatar
Guido van Rossum committed
68 69 70
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.
71
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
72

73
\begin{methoddesc}{send}{data}
Guido van Rossum's avatar
Guido van Rossum committed
74
Send data to the server.  This should be used directly only after the
Fred Drake's avatar
Fred Drake committed
75 76
\method{endheaders()} method has been called and before
\method{getreply()} has been called.
77
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
78

79
\begin{methoddesc}{putrequest}{request, selector}
Guido van Rossum's avatar
Guido van Rossum committed
80 81 82 83
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}).
84
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
85

86
\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
87
Send an \rfc{822} style header to the server.  It sends a line to the
Guido van Rossum's avatar
Guido van Rossum committed
88 89 90
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.
91
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
92

93
\begin{methoddesc}{endheaders}{}
Guido van Rossum's avatar
Guido van Rossum committed
94
Send a blank line to the server, signalling the end of the headers.
95
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
96

97
\begin{methoddesc}{getreply}{}
Guido van Rossum's avatar
Guido van Rossum committed
98
Complete the request by shutting down the sending end of the socket,
Fred Drake's avatar
Fred Drake committed
99 100
read the reply from the server, and return a triple
\code{(\var{replycode}, \var{message}, \var{headers})}.  Here,
101
\var{replycode} is the integer reply code from the request (e.g.,
Fred Drake's avatar
Fred Drake committed
102 103 104 105 106
\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
\module{mimetools}\refstmodindex{mimetools} module.
107
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
108

109
\begin{methoddesc}{getfile}{}
Guido van Rossum's avatar
Guido van Rossum committed
110
Return a file object from which the data returned by the server can be
Fred Drake's avatar
Fred Drake committed
111 112
read, using the \method{read()}, \method{readline()} or
\method{readlines()} methods.
113
\end{methoddesc}
114 115

\subsection{Example}
116
\nodename{HTTP Example}
117 118 119

Here is an example session:

120
\begin{verbatim}
121 122 123 124 125 126 127 128 129
>>> 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()
130
>>> data = f.read() # Get the raw HTML
131
>>> f.close()
132
\end{verbatim}