1 \section{Standard Module
\sectcode{httplib
}}
5 \renewcommand{\indexsubitem}{(in module httplib)
}
7 This module defines a class which implements the client side of the
8 HTTP protocol. It is normally not used directly --- the module
9 \code{urllib
} uses it to handle URLs that use HTTP.
12 The module defines one class,
\code{HTTP
}. An
\code{HTTP
} instance
13 represents one transaction with an HTTP server. It should be
14 instantiated passing it a host and optional port number. If no port
15 number is passed, the port is extracted from the host string if it has
16 the form
\code{host:port
}, else the default HTTP port (
80) is used.
17 If no host is passed, no connection is made, and the
\code{connect
}
18 method should be used to connect to a server. For example, the
19 following calls all create instances that connect to the server at the
23 >>> h1 = httplib.HTTP('www.cwi.nl')
24 >>> h2 = httplib.HTTP('www.cwi.nl:
80')
25 >>> h3 = httplib.HTTP('www.cwi.nl',
80)
28 Once an
\code{HTTP
} instance has been connected to an HTTP server, it
29 should be used as follows:
33 \item[1.
] Make exactly one call to the
\code{putrequest()
} method.
35 \item[2.
] Make zero or more calls to the
\code{putheader()
} method.
37 \item[3.
] Call the
\code{endheaders()
} method (this can be omitted if
38 step
4 makes no calls).
40 \item[4.
] Optional calls to the
\code{send()
} method.
42 \item[5.
] Call the
\code{getreply()
} method.
44 \item[6.
] Call the
\code{getfile()
} method and read the data off the
45 file object that it returns.
49 \subsection{HTTP Objects
}
51 \code{HTTP
} instances have the following methods:
53 \renewcommand{\indexsubitem}{(HTTP method)
}
55 \begin{funcdesc
}{set_debuglevel
}{level
}
56 Set the debugging level (the amount of debugging output printed).
57 The default debug level is
\code{0}, meaning no debugging output is
61 \begin{funcdesc
}{connect
}{host
\optional{\, port
}}
62 Connect to the server given by
\var{host
} and
\var{port
}. See the
63 intro for the default port. This should be called directly only if
64 the instance was instantiated without passing a host.
67 \begin{funcdesc
}{send
}{data
}
68 Send data to the server. This should be used directly only after the
69 \code{endheaders()
} method has been called and before
70 \code{getreply()
} has been called.
73 \begin{funcdesc
}{putrequest
}{request\, selector
}
74 This should be the first call after the connection to the server has
75 been made. It sends a line to the server consisting of the
76 \var{request
} string, the
\var{selector
} string, and the HTTP version
80 \begin{funcdesc
}{putheader
}{header\, argument
\optional{\, ...
}}
81 Send an RFC-
822 style header to the server. It sends a line to the
82 server consisting of the header, a colon and a space, and the first
83 argument. If more arguments are given, continuation lines are sent,
84 each consisting of a tab and an argument.
87 \begin{funcdesc
}{endheaders
}{}
88 Send a blank line to the server, signalling the end of the headers.
91 \begin{funcdesc
}{getreply
}{}
92 Complete the request by shutting down the sending end of the socket,
93 read the reply from the server, and return a triple (
\var{replycode
},
94 \var{message
},
\var{headers
}). Here
\var{replycode
} is the integer
95 reply code from the request (e.g.\
\code{200} if the request was
96 handled properly);
\var{message
} is the message string corresponding
97 to the reply code; and
\var{header
} is an instance of the class
98 \code{rfc822.Message
} containing the headers received from the server.
99 See the description of the
\code{rfc822
} module.
103 \begin{funcdesc
}{getfile
}{}
104 Return a file object from which the data returned by the server can be
105 read, using the
\code{read()
},
\code{readline()
} or
\code{readlines()
}
110 \nodename{HTTP Example
}
112 Here is an example session:
116 >>> h = httplib.HTTP('www.cwi.nl')
117 >>> h.putrequest('GET', '/index.html')
118 >>> h.putheader('Accept', 'text/html')
119 >>> h.putheader('Accept', 'text/plain')
121 >>> errcode, errmsg, headers = h.getreply()
122 >>> print errcode # Should be
200
124 >>> data f.read() # Get the raw HTML