blob: a284faa1faf70c7d24a630b425e00673dee2f5db [file] [log] [blame]
Guido van Rossuma12ef941995-02-27 17:53:25 +00001\section{Built-in module \sectcode{httplib}}
2\stmodindex{httplib}
3\index{HTTP}
4
5This module defines a class which implements the client side of the
6HTTP protocol. It is normally not used directly --- the module
7\code{urlllib} module uses it to handle URLs that use HTTP.
8\stmodindex{urllib}
9
10The module defines one class, \code{HTTP}. An \code{HTTP} instance
11represents one transaction with an HTTP server. It should be
12instantiated passing it a host and optional port number. If no port
13number is passed, the port is extracted from the host string if it has
14the form \code{host:port}, else the default HTTP port (80) is used.
15If no host is passed, no connection is made, and the \code{connect}
16method should be used to connect to a server.
17
18Once an \code{HTTP} instance has been connected to an HTTP server, it
19should be used as follows:
20
21\begin{enumerate}
22
23\item[1.] Make exactly one call to the \code{putrequest()} method.
24
25\item[2.] Make zero or more calls to the \code{putheader()} method.
26
27\item[3.] Call the \code{endheaders()} method (this can be omitted if
28step 4. makes no calls).
29
30\item[4.] Optional calls to the \code{send()} method.
31
32\item[5.] Call the \code{getreply()} method.
33
34\item[6.] Call the \code{getfile()} method and read the data off the
35file object that it returns.
36
37\end{enumerate}
38
39\code{HTTP} instances have the following methods:
40
41\begin{funcdesc}{set_debuglevel}{level}
42Set the debugging level (the amount of debugging output printed).
43The default debug level is \code{0}, meaning no debugging output is
44printed.
45\end{funcdesc}
46
47\begin{funcdesc}{connect}{host\optional{\, port}}
48Connect to the server given by \var{host} and \var{port}. See the
49intro for the default port. This should be called directly only if
50the instance was instantiated without passing a host.
51\end{funcdesc}
52
53\begin{funcdesc}{send}{data}
54Send data to the server. This should be used directly only after the
55\code{endheaders()} method has been called and before
56\code{getreply()} has been called.
57\end{funcdesc}
58
59\begin{funcdesc}{putrequest}{request\, selector}
60This should be the first call after the connection to the server has
61been made. It sends a line to the server consisting of the
62\var{request} string, the \var{selector} string, and the HTTP version
63(\code{HTTP/1.0}).
64\end{funcdesc}
65
66\begin{funcdesc}{putheader}{header\, argument\optional{\, ...}}
67Send an RFC-822 style header to the server. It sends a line to the
68server consisting of the header, a colon and a space, and the first
69argument. If more arguments are given, continuation lines are sent,
70each consisting of a tab and an argument.
71\end{funcdesc}
72
73\begin{funcdesc}{endheaders}{}
74Send a blank line to the server, signalling the end of the headers.
75\end{funcdesc}
76
77\begin{funcdesc}{getreply}{}
78Complete the request by shutting down the sending end of the socket,
79read the reply from the server, and return a triple (\var{replycode},
80\var{message}, \var{headers}). Here \var{replycode} is the integer
81reply code from the request (e.g. \code{200} if the request was
82handled properly); \var{message} is the message string corresponding
83to the reply code; and \var{header} is an instance of the class
84\code{rfc822.Message} containing the headers received from the server.
85See the description of the \code{rfc822} module.
86\stmodindex{rfc822}
87\end{funcdesc}
88
89\begin{funcdesc}{getfile}{}
90Return a file object from which the data returned by the server can be
91read, using the \code{read()}, \code{readline()} or \code{readlines()}
92methods.
93\end{funcdesc}