1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
\section{\module{httplib} ---
HTTP protocol client}
\declaremodule{standard}{httplib}
\modulesynopsis{HTTP and HTTPS protocol client (requires sockets).}
\indexii{HTTP}{protocol}
\index{HTTP!\module{httplib} (standard module)}
This module defines classes which implement the client side of the
HTTP and HTTPS protocols. It is normally not used directly --- the
module \refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs
that use HTTP and HTTPS.
\begin{notice}
HTTPS support is only available if the \refmodule{socket} module was
compiled with SSL support.
\end{notice}
\begin{notice}
The public interface for this module changed substantially in Python
2.0. The \class{HTTP} class is retained only for backward
compatibility with 1.5.2. It should not be used in new code. Refer
to the online docstrings for usage.
\end{notice}
The constants defined in this module are:
\begin{datadesc}{HTTP_PORT}
The default port for the HTTP protocol (always \code{80}).
\end{datadesc}
\begin{datadesc}{HTTPS_PORT}
The default port for the HTTPS protocol (always \code{443}).
\end{datadesc}
The module provides the following classes:
\begin{classdesc}{HTTPConnection}{host\optional{, port}}
An \class{HTTPConnection} instance 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. For example, the following calls all create instances that connect to
the server at the same host and port:
\begin{verbatim}
>>> h1 = httplib.HTTPConnection('www.cwi.nl')
>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
\end{verbatim}
\versionadded{2.0}
\end{classdesc}
\begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
A subclass of \class{HTTPConnection} that uses SSL for communication with
secure servers. Default port is \code{443}.
\var{key_file} is
the name of a PEM formatted file that contains your private
key. \var{cert_file} is a PEM formatted certificate chain file.
\warning{This does not do any certificate verification!}
\versionadded{2.0}
\end{classdesc}
\begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
Class whose instances are returned upon successful connection. Not
instantiated directly by user.
\versionadded{2.0}
\end{classdesc}
The following exceptions are raised as appropriate:
\begin{excdesc}{HTTPException}
The base class of the other exceptions in this module. It is a
subclass of \exception{Exception}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{NotConnected}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{InvalidURL}
A subclass of \exception{HTTPException}, raised if a port is given and is
either non-numeric or empty.
\versionadded{2.3}
\end{excdesc}
\begin{excdesc}{UnknownProtocol}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{UnknownTransferEncoding}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{UnimplementedFileMode}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{IncompleteRead}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{ImproperConnectionState}
A subclass of \exception{HTTPException}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{CannotSendRequest}
A subclass of \exception{ImproperConnectionState}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{CannotSendHeader}
A subclass of \exception{ImproperConnectionState}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{ResponseNotReady}
A subclass of \exception{ImproperConnectionState}.
\versionadded{2.0}
\end{excdesc}
\begin{excdesc}{BadStatusLine}
A subclass of \exception{HTTPException}. Raised if a server responds with a
HTTP status code that we don't understand.
\versionadded{2.0}
\end{excdesc}
\subsection{HTTPConnection Objects \label{httpconnection-objects}}
\class{HTTPConnection} instances have the following methods:
\begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
This will send a request to the server using the HTTP request method
\var{method} and the selector \var{url}. If the \var{body} argument is
present, it should be a string of data to send after the headers are finished.
The header Content-Length is automatically set to the correct value.
The \var{headers} argument should be a mapping of extra HTTP headers to send
with the request.
\end{methoddesc}
\begin{methoddesc}{getresponse}{}
Should be called after a request is sent to get the response from the server.
Returns an \class{HTTPResponse} instance.
\end{methoddesc}
\begin{methoddesc}{set_debuglevel}{level}
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}
\begin{methoddesc}{connect}{}
Connect to the server specified when the object was created.
\end{methoddesc}
\begin{methoddesc}{close}{}
Close the connection to the server.
\end{methoddesc}
\begin{methoddesc}{send}{data}
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}
\begin{methoddesc}{putrequest}{request, selector}
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.1}).
\end{methoddesc}
\begin{methoddesc}{putheader}{header, argument\optional{, ...}}
Send an \rfc{822}-style header to the server. It sends a line to the
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}
\begin{methoddesc}{endheaders}{}
Send a blank line to the server, signalling the end of the headers.
\end{methoddesc}
\subsection{HTTPResponse Objects \label{httpresponse-objects}}
\class{HTTPResponse} instances have the following methods and attributes:
\begin{methoddesc}{read}{}
Reads and returns the response body.
\end{methoddesc}
\begin{methoddesc}{getheader}{name\optional{, default}}
Get the contents of the header \var{name}, or \var{default} if there is no
matching header.
\end{methoddesc}
\begin{datadesc}{msg}
A \class{mimetools.Message} instance containing the response headers.
\end{datadesc}
\begin{datadesc}{version}
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
\end{datadesc}
\begin{datadesc}{status}
Status code returned by server.
\end{datadesc}
\begin{datadesc}{reason}
Reason phrase returned by server.
\end{datadesc}
\subsection{Examples \label{httplib-examples}}
Here is an example session that uses the \samp{GET} method:
\begin{verbatim}
>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> conn.request("GET", "/index.html")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> data1 = r1.read()
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print r2.status, r2.reason
404 Not Found
>>> data2 = r2.read()
>>> conn.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})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
... "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
200 OK
>>> data = response.read()
>>> conn.close()
\end{verbatim}
|