| 1 | \section{\module{httplib} ---
|
|---|
| 2 | HTTP protocol client}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{httplib}
|
|---|
| 5 | \modulesynopsis{HTTP and HTTPS protocol client (requires sockets).}
|
|---|
| 6 |
|
|---|
| 7 | \indexii{HTTP}{protocol}
|
|---|
| 8 | \index{HTTP!\module{httplib} (standard module)}
|
|---|
| 9 |
|
|---|
| 10 | This module defines classes which implement the client side of the
|
|---|
| 11 | HTTP and HTTPS protocols. It is normally not used directly --- the
|
|---|
| 12 | module \refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs
|
|---|
| 13 | that use HTTP and HTTPS.
|
|---|
| 14 |
|
|---|
| 15 | \begin{notice}
|
|---|
| 16 | HTTPS support is only available if the \refmodule{socket} module was
|
|---|
| 17 | compiled with SSL support.
|
|---|
| 18 | \end{notice}
|
|---|
| 19 |
|
|---|
| 20 | \begin{notice}
|
|---|
| 21 | The public interface for this module changed substantially in Python
|
|---|
| 22 | 2.0. The \class{HTTP} class is retained only for backward
|
|---|
| 23 | compatibility with 1.5.2. It should not be used in new code. Refer
|
|---|
| 24 | to the online docstrings for usage.
|
|---|
| 25 | \end{notice}
|
|---|
| 26 |
|
|---|
| 27 | The module provides the following classes:
|
|---|
| 28 |
|
|---|
| 29 | \begin{classdesc}{HTTPConnection}{host\optional{, port}}
|
|---|
| 30 | An \class{HTTPConnection} instance represents one transaction with an HTTP
|
|---|
| 31 | server. It should be instantiated passing it a host and optional port number.
|
|---|
| 32 | If no port number is passed, the port is extracted from the host string if it
|
|---|
| 33 | has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is
|
|---|
| 34 | used. For example, the following calls all create instances that connect to
|
|---|
| 35 | the server at the same host and port:
|
|---|
| 36 |
|
|---|
| 37 | \begin{verbatim}
|
|---|
| 38 | >>> h1 = httplib.HTTPConnection('www.cwi.nl')
|
|---|
| 39 | >>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
|
|---|
| 40 | >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
|
|---|
| 41 | \end{verbatim}
|
|---|
| 42 | \versionadded{2.0}
|
|---|
| 43 | \end{classdesc}
|
|---|
| 44 |
|
|---|
| 45 | \begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}}
|
|---|
| 46 | A subclass of \class{HTTPConnection} that uses SSL for communication with
|
|---|
| 47 | secure servers. Default port is \code{443}.
|
|---|
| 48 | \var{key_file} is
|
|---|
| 49 | the name of a PEM formatted file that contains your private
|
|---|
| 50 | key. \var{cert_file} is a PEM formatted certificate chain file.
|
|---|
| 51 |
|
|---|
| 52 | \warning{This does not do any certificate verification!}
|
|---|
| 53 |
|
|---|
| 54 | \versionadded{2.0}
|
|---|
| 55 | \end{classdesc}
|
|---|
| 56 |
|
|---|
| 57 | \begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}}
|
|---|
| 58 | Class whose instances are returned upon successful connection. Not
|
|---|
| 59 | instantiated directly by user.
|
|---|
| 60 | \versionadded{2.0}
|
|---|
| 61 | \end{classdesc}
|
|---|
| 62 |
|
|---|
| 63 | The following exceptions are raised as appropriate:
|
|---|
| 64 |
|
|---|
| 65 | \begin{excdesc}{HTTPException}
|
|---|
| 66 | The base class of the other exceptions in this module. It is a
|
|---|
| 67 | subclass of \exception{Exception}.
|
|---|
| 68 | \versionadded{2.0}
|
|---|
| 69 | \end{excdesc}
|
|---|
| 70 |
|
|---|
| 71 | \begin{excdesc}{NotConnected}
|
|---|
| 72 | A subclass of \exception{HTTPException}.
|
|---|
| 73 | \versionadded{2.0}
|
|---|
| 74 | \end{excdesc}
|
|---|
| 75 |
|
|---|
| 76 | \begin{excdesc}{InvalidURL}
|
|---|
| 77 | A subclass of \exception{HTTPException}, raised if a port is given and is
|
|---|
| 78 | either non-numeric or empty.
|
|---|
| 79 | \versionadded{2.3}
|
|---|
| 80 | \end{excdesc}
|
|---|
| 81 |
|
|---|
| 82 | \begin{excdesc}{UnknownProtocol}
|
|---|
| 83 | A subclass of \exception{HTTPException}.
|
|---|
| 84 | \versionadded{2.0}
|
|---|
| 85 | \end{excdesc}
|
|---|
| 86 |
|
|---|
| 87 | \begin{excdesc}{UnknownTransferEncoding}
|
|---|
| 88 | A subclass of \exception{HTTPException}.
|
|---|
| 89 | \versionadded{2.0}
|
|---|
| 90 | \end{excdesc}
|
|---|
| 91 |
|
|---|
| 92 | \begin{excdesc}{UnimplementedFileMode}
|
|---|
| 93 | A subclass of \exception{HTTPException}.
|
|---|
| 94 | \versionadded{2.0}
|
|---|
| 95 | \end{excdesc}
|
|---|
| 96 |
|
|---|
| 97 | \begin{excdesc}{IncompleteRead}
|
|---|
| 98 | A subclass of \exception{HTTPException}.
|
|---|
| 99 | \versionadded{2.0}
|
|---|
| 100 | \end{excdesc}
|
|---|
| 101 |
|
|---|
| 102 | \begin{excdesc}{ImproperConnectionState}
|
|---|
| 103 | A subclass of \exception{HTTPException}.
|
|---|
| 104 | \versionadded{2.0}
|
|---|
| 105 | \end{excdesc}
|
|---|
| 106 |
|
|---|
| 107 | \begin{excdesc}{CannotSendRequest}
|
|---|
| 108 | A subclass of \exception{ImproperConnectionState}.
|
|---|
| 109 | \versionadded{2.0}
|
|---|
| 110 | \end{excdesc}
|
|---|
| 111 |
|
|---|
| 112 | \begin{excdesc}{CannotSendHeader}
|
|---|
| 113 | A subclass of \exception{ImproperConnectionState}.
|
|---|
| 114 | \versionadded{2.0}
|
|---|
| 115 | \end{excdesc}
|
|---|
| 116 |
|
|---|
| 117 | \begin{excdesc}{ResponseNotReady}
|
|---|
| 118 | A subclass of \exception{ImproperConnectionState}.
|
|---|
| 119 | \versionadded{2.0}
|
|---|
| 120 | \end{excdesc}
|
|---|
| 121 |
|
|---|
| 122 | \begin{excdesc}{BadStatusLine}
|
|---|
| 123 | A subclass of \exception{HTTPException}. Raised if a server responds with a
|
|---|
| 124 | HTTP status code that we don't understand.
|
|---|
| 125 | \versionadded{2.0}
|
|---|
| 126 | \end{excdesc}
|
|---|
| 127 |
|
|---|
| 128 | The constants defined in this module are:
|
|---|
| 129 |
|
|---|
| 130 | \begin{datadesc}{HTTP_PORT}
|
|---|
| 131 | The default port for the HTTP protocol (always \code{80}).
|
|---|
| 132 | \end{datadesc}
|
|---|
| 133 |
|
|---|
| 134 | \begin{datadesc}{HTTPS_PORT}
|
|---|
| 135 | The default port for the HTTPS protocol (always \code{443}).
|
|---|
| 136 | \end{datadesc}
|
|---|
| 137 |
|
|---|
| 138 | and also the following constants for integer status codes:
|
|---|
| 139 |
|
|---|
| 140 | \begin{tableiii}{l|c|l}{constant}{Constant}{Value}{Definition}
|
|---|
| 141 | \lineiii{CONTINUE}{\code{100}}
|
|---|
| 142 | {HTTP/1.1, \ulink{RFC 2616, Section 10.1.1}
|
|---|
| 143 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1}}
|
|---|
| 144 | \lineiii{SWITCHING_PROTOCOLS}{\code{101}}
|
|---|
| 145 | {HTTP/1.1, \ulink{RFC 2616, Section 10.1.2}
|
|---|
| 146 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2}}
|
|---|
| 147 | \lineiii{PROCESSING}{\code{102}}
|
|---|
| 148 | {WEBDAV, \ulink{RFC 2518, Section 10.1}
|
|---|
| 149 | {http://www.webdav.org/specs/rfc2518.html#STATUS_102}}
|
|---|
| 150 |
|
|---|
| 151 | \lineiii{OK}{\code{200}}
|
|---|
| 152 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.1}
|
|---|
| 153 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1}}
|
|---|
| 154 | \lineiii{CREATED}{\code{201}}
|
|---|
| 155 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.2}
|
|---|
| 156 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2}}
|
|---|
| 157 | \lineiii{ACCEPTED}{\code{202}}
|
|---|
| 158 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.3}
|
|---|
| 159 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3}}
|
|---|
| 160 | \lineiii{NON_AUTHORITATIVE_INFORMATION}{\code{203}}
|
|---|
| 161 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.4}
|
|---|
| 162 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4}}
|
|---|
| 163 | \lineiii{NO_CONTENT}{\code{204}}
|
|---|
| 164 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.5}
|
|---|
| 165 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5}}
|
|---|
| 166 | \lineiii{RESET_CONTENT}{\code{205}}
|
|---|
| 167 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.6}
|
|---|
| 168 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6}}
|
|---|
| 169 | \lineiii{PARTIAL_CONTENT}{\code{206}}
|
|---|
| 170 | {HTTP/1.1, \ulink{RFC 2616, Section 10.2.7}
|
|---|
| 171 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7}}
|
|---|
| 172 | \lineiii{MULTI_STATUS}{\code{207}}
|
|---|
| 173 | {WEBDAV \ulink{RFC 2518, Section 10.2}
|
|---|
| 174 | {http://www.webdav.org/specs/rfc2518.html#STATUS_207}}
|
|---|
| 175 | \lineiii{IM_USED}{\code{226}}
|
|---|
| 176 | {Delta encoding in HTTP, \rfc{3229}, Section 10.4.1}
|
|---|
| 177 |
|
|---|
| 178 | \lineiii{MULTIPLE_CHOICES}{\code{300}}
|
|---|
| 179 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.1}
|
|---|
| 180 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1}}
|
|---|
| 181 | \lineiii{MOVED_PERMANENTLY}{\code{301}}
|
|---|
| 182 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.2}
|
|---|
| 183 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2}}
|
|---|
| 184 | \lineiii{FOUND}{\code{302}}
|
|---|
| 185 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.3}
|
|---|
| 186 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3}}
|
|---|
| 187 | \lineiii{SEE_OTHER}{\code{303}}
|
|---|
| 188 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.4}
|
|---|
| 189 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4}}
|
|---|
| 190 | \lineiii{NOT_MODIFIED}{\code{304}}
|
|---|
| 191 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.5}
|
|---|
| 192 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5}}
|
|---|
| 193 | \lineiii{USE_PROXY}{\code{305}}
|
|---|
| 194 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.6}
|
|---|
| 195 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6}}
|
|---|
| 196 | \lineiii{TEMPORARY_REDIRECT}{\code{307}}
|
|---|
| 197 | {HTTP/1.1, \ulink{RFC 2616, Section 10.3.8}
|
|---|
| 198 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8}}
|
|---|
| 199 |
|
|---|
| 200 | \lineiii{BAD_REQUEST}{\code{400}}
|
|---|
| 201 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.1}
|
|---|
| 202 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1}}
|
|---|
| 203 | \lineiii{UNAUTHORIZED}{\code{401}}
|
|---|
| 204 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.2}
|
|---|
| 205 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2}}
|
|---|
| 206 | \lineiii{PAYMENT_REQUIRED}{\code{402}}
|
|---|
| 207 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.3}
|
|---|
| 208 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3}}
|
|---|
| 209 | \lineiii{FORBIDDEN}{\code{403}}
|
|---|
| 210 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.4}
|
|---|
| 211 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4}}
|
|---|
| 212 | \lineiii{NOT_FOUND}{\code{404}}
|
|---|
| 213 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.5}
|
|---|
| 214 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5}}
|
|---|
| 215 | \lineiii{METHOD_NOT_ALLOWED}{\code{405}}
|
|---|
| 216 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.6}
|
|---|
| 217 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6}}
|
|---|
| 218 | \lineiii{NOT_ACCEPTABLE}{\code{406}}
|
|---|
| 219 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.7}
|
|---|
| 220 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7}}
|
|---|
| 221 | \lineiii{PROXY_AUTHENTICATION_REQUIRED}
|
|---|
| 222 | {\code{407}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.8}
|
|---|
| 223 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8}}
|
|---|
| 224 | \lineiii{REQUEST_TIMEOUT}{\code{408}}
|
|---|
| 225 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.9}
|
|---|
| 226 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9}}
|
|---|
| 227 | \lineiii{CONFLICT}{\code{409}}
|
|---|
| 228 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.10}
|
|---|
| 229 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10}}
|
|---|
| 230 | \lineiii{GONE}{\code{410}}
|
|---|
| 231 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.11}
|
|---|
| 232 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11}}
|
|---|
| 233 | \lineiii{LENGTH_REQUIRED}{\code{411}}
|
|---|
| 234 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.12}
|
|---|
| 235 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12}}
|
|---|
| 236 | \lineiii{PRECONDITION_FAILED}{\code{412}}
|
|---|
| 237 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.13}
|
|---|
| 238 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13}}
|
|---|
| 239 | \lineiii{REQUEST_ENTITY_TOO_LARGE}
|
|---|
| 240 | {\code{413}}{HTTP/1.1, \ulink{RFC 2616, Section 10.4.14}
|
|---|
| 241 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14}}
|
|---|
| 242 | \lineiii{REQUEST_URI_TOO_LONG}{\code{414}}
|
|---|
| 243 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.15}
|
|---|
| 244 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15}}
|
|---|
| 245 | \lineiii{UNSUPPORTED_MEDIA_TYPE}{\code{415}}
|
|---|
| 246 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.16}
|
|---|
| 247 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16}}
|
|---|
| 248 | \lineiii{REQUESTED_RANGE_NOT_SATISFIABLE}{\code{416}}
|
|---|
| 249 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.17}
|
|---|
| 250 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17}}
|
|---|
| 251 | \lineiii{EXPECTATION_FAILED}{\code{417}}
|
|---|
| 252 | {HTTP/1.1, \ulink{RFC 2616, Section 10.4.18}
|
|---|
| 253 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18}}
|
|---|
| 254 | \lineiii{UNPROCESSABLE_ENTITY}{\code{422}}
|
|---|
| 255 | {WEBDAV, \ulink{RFC 2518, Section 10.3}
|
|---|
| 256 | {http://www.webdav.org/specs/rfc2518.html#STATUS_422}}
|
|---|
| 257 | \lineiii{LOCKED}{\code{423}}
|
|---|
| 258 | {WEBDAV \ulink{RFC 2518, Section 10.4}
|
|---|
| 259 | {http://www.webdav.org/specs/rfc2518.html#STATUS_423}}
|
|---|
| 260 | \lineiii{FAILED_DEPENDENCY}{\code{424}}
|
|---|
| 261 | {WEBDAV, \ulink{RFC 2518, Section 10.5}
|
|---|
| 262 | {http://www.webdav.org/specs/rfc2518.html#STATUS_424}}
|
|---|
| 263 | \lineiii{UPGRADE_REQUIRED}{\code{426}}
|
|---|
| 264 | {HTTP Upgrade to TLS, \rfc{2817}, Section 6}
|
|---|
| 265 |
|
|---|
| 266 | \lineiii{INTERNAL_SERVER_ERROR}{\code{500}}
|
|---|
| 267 | {HTTP/1.1, \ulink{RFC 2616, Section 10.5.1}
|
|---|
| 268 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1}}
|
|---|
| 269 | \lineiii{NOT_IMPLEMENTED}{\code{501}}
|
|---|
| 270 | {HTTP/1.1, \ulink{RFC 2616, Section 10.5.2}
|
|---|
| 271 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2}}
|
|---|
| 272 | \lineiii{BAD_GATEWAY}{\code{502}}
|
|---|
| 273 | {HTTP/1.1 \ulink{RFC 2616, Section 10.5.3}
|
|---|
| 274 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3}}
|
|---|
| 275 | \lineiii{SERVICE_UNAVAILABLE}{\code{503}}
|
|---|
| 276 | {HTTP/1.1, \ulink{RFC 2616, Section 10.5.4}
|
|---|
| 277 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4}}
|
|---|
| 278 | \lineiii{GATEWAY_TIMEOUT}{\code{504}}
|
|---|
| 279 | {HTTP/1.1 \ulink{RFC 2616, Section 10.5.5}
|
|---|
| 280 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5}}
|
|---|
| 281 | \lineiii{HTTP_VERSION_NOT_SUPPORTED}{\code{505}}
|
|---|
| 282 | {HTTP/1.1, \ulink{RFC 2616, Section 10.5.6}
|
|---|
| 283 | {http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6}}
|
|---|
| 284 | \lineiii{INSUFFICIENT_STORAGE}{\code{507}}
|
|---|
| 285 | {WEBDAV, \ulink{RFC 2518, Section 10.6}
|
|---|
| 286 | {http://www.webdav.org/specs/rfc2518.html#STATUS_507}}
|
|---|
| 287 | \lineiii{NOT_EXTENDED}{\code{510}}
|
|---|
| 288 | {An HTTP Extension Framework, \rfc{2774}, Section 7}
|
|---|
| 289 | \end{tableiii}
|
|---|
| 290 |
|
|---|
| 291 | \begin{datadesc}{responses}
|
|---|
| 292 | This dictionary maps the HTTP 1.1 status codes to the W3C names.
|
|---|
| 293 |
|
|---|
| 294 | Example: \code{httplib.responses[httplib.NOT_FOUND]} is \code{'Not Found'}.
|
|---|
| 295 | \versionadded{2.5}
|
|---|
| 296 | \end{datadesc}
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 | \subsection{HTTPConnection Objects \label{httpconnection-objects}}
|
|---|
| 300 |
|
|---|
| 301 | \class{HTTPConnection} instances have the following methods:
|
|---|
| 302 |
|
|---|
| 303 | \begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}}
|
|---|
| 304 | This will send a request to the server using the HTTP request method
|
|---|
| 305 | \var{method} and the selector \var{url}. If the \var{body} argument is
|
|---|
| 306 | present, it should be a string of data to send after the headers are finished.
|
|---|
| 307 | The header Content-Length is automatically set to the correct value.
|
|---|
| 308 | The \var{headers} argument should be a mapping of extra HTTP headers to send
|
|---|
| 309 | with the request.
|
|---|
| 310 | \end{methoddesc}
|
|---|
| 311 |
|
|---|
| 312 | \begin{methoddesc}{getresponse}{}
|
|---|
| 313 | Should be called after a request is sent to get the response from the server.
|
|---|
| 314 | Returns an \class{HTTPResponse} instance.
|
|---|
| 315 | \note{Note that you must have read the whole response before you can send a new
|
|---|
| 316 | request to the server.}
|
|---|
| 317 | \end{methoddesc}
|
|---|
| 318 |
|
|---|
| 319 | \begin{methoddesc}{set_debuglevel}{level}
|
|---|
| 320 | Set the debugging level (the amount of debugging output printed).
|
|---|
| 321 | The default debug level is \code{0}, meaning no debugging output is
|
|---|
| 322 | printed.
|
|---|
| 323 | \end{methoddesc}
|
|---|
| 324 |
|
|---|
| 325 | \begin{methoddesc}{connect}{}
|
|---|
| 326 | Connect to the server specified when the object was created.
|
|---|
| 327 | \end{methoddesc}
|
|---|
| 328 |
|
|---|
| 329 | \begin{methoddesc}{close}{}
|
|---|
| 330 | Close the connection to the server.
|
|---|
| 331 | \end{methoddesc}
|
|---|
| 332 |
|
|---|
| 333 | As an alternative to using the \method{request()} method described above,
|
|---|
| 334 | you can also send your request step by step, by using the four functions
|
|---|
| 335 | below.
|
|---|
| 336 |
|
|---|
| 337 | \begin{methoddesc}{putrequest}{request, selector\optional{,
|
|---|
| 338 | skip\_host\optional{, skip_accept_encoding}}}
|
|---|
| 339 | This should be the first call after the connection to the server has
|
|---|
| 340 | been made. It sends a line to the server consisting of the
|
|---|
| 341 | \var{request} string, the \var{selector} string, and the HTTP version
|
|---|
| 342 | (\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or
|
|---|
| 343 | \code{Accept-Encoding:} headers (for example to accept additional
|
|---|
| 344 | content encodings), specify \var{skip_host} or \var{skip_accept_encoding}
|
|---|
| 345 | with non-False values.
|
|---|
| 346 | \versionchanged[\var{skip_accept_encoding} argument added]{2.4}
|
|---|
| 347 | \end{methoddesc}
|
|---|
| 348 |
|
|---|
| 349 | \begin{methoddesc}{putheader}{header, argument\optional{, ...}}
|
|---|
| 350 | Send an \rfc{822}-style header to the server. It sends a line to the
|
|---|
| 351 | server consisting of the header, a colon and a space, and the first
|
|---|
| 352 | argument. If more arguments are given, continuation lines are sent,
|
|---|
| 353 | each consisting of a tab and an argument.
|
|---|
| 354 | \end{methoddesc}
|
|---|
| 355 |
|
|---|
| 356 | \begin{methoddesc}{endheaders}{}
|
|---|
| 357 | Send a blank line to the server, signalling the end of the headers.
|
|---|
| 358 | \end{methoddesc}
|
|---|
| 359 |
|
|---|
| 360 | \begin{methoddesc}{send}{data}
|
|---|
| 361 | Send data to the server. This should be used directly only after the
|
|---|
| 362 | \method{endheaders()} method has been called and before
|
|---|
| 363 | \method{getresponse()} is called.
|
|---|
| 364 | \end{methoddesc}
|
|---|
| 365 |
|
|---|
| 366 | \subsection{HTTPResponse Objects \label{httpresponse-objects}}
|
|---|
| 367 |
|
|---|
| 368 | \class{HTTPResponse} instances have the following methods and attributes:
|
|---|
| 369 |
|
|---|
| 370 | \begin{methoddesc}{read}{\optional{amt}}
|
|---|
| 371 | Reads and returns the response body, or up to the next \var{amt} bytes.
|
|---|
| 372 | \end{methoddesc}
|
|---|
| 373 |
|
|---|
| 374 | \begin{methoddesc}{getheader}{name\optional{, default}}
|
|---|
| 375 | Get the contents of the header \var{name}, or \var{default} if there is no
|
|---|
| 376 | matching header.
|
|---|
| 377 | \end{methoddesc}
|
|---|
| 378 |
|
|---|
| 379 | \begin{methoddesc}{getheaders}{}
|
|---|
| 380 | Return a list of (header, value) tuples. \versionadded{2.4}
|
|---|
| 381 | \end{methoddesc}
|
|---|
| 382 |
|
|---|
| 383 | \begin{datadesc}{msg}
|
|---|
| 384 | A \class{mimetools.Message} instance containing the response headers.
|
|---|
| 385 | \end{datadesc}
|
|---|
| 386 |
|
|---|
| 387 | \begin{datadesc}{version}
|
|---|
| 388 | HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
|
|---|
| 389 | \end{datadesc}
|
|---|
| 390 |
|
|---|
| 391 | \begin{datadesc}{status}
|
|---|
| 392 | Status code returned by server.
|
|---|
| 393 | \end{datadesc}
|
|---|
| 394 |
|
|---|
| 395 | \begin{datadesc}{reason}
|
|---|
| 396 | Reason phrase returned by server.
|
|---|
| 397 | \end{datadesc}
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 | \subsection{Examples \label{httplib-examples}}
|
|---|
| 401 |
|
|---|
| 402 | Here is an example session that uses the \samp{GET} method:
|
|---|
| 403 |
|
|---|
| 404 | \begin{verbatim}
|
|---|
| 405 | >>> import httplib
|
|---|
| 406 | >>> conn = httplib.HTTPConnection("www.python.org")
|
|---|
| 407 | >>> conn.request("GET", "/index.html")
|
|---|
| 408 | >>> r1 = conn.getresponse()
|
|---|
| 409 | >>> print r1.status, r1.reason
|
|---|
| 410 | 200 OK
|
|---|
| 411 | >>> data1 = r1.read()
|
|---|
| 412 | >>> conn.request("GET", "/parrot.spam")
|
|---|
| 413 | >>> r2 = conn.getresponse()
|
|---|
| 414 | >>> print r2.status, r2.reason
|
|---|
| 415 | 404 Not Found
|
|---|
| 416 | >>> data2 = r2.read()
|
|---|
| 417 | >>> conn.close()
|
|---|
| 418 | \end{verbatim}
|
|---|
| 419 |
|
|---|
| 420 | Here is an example session that shows how to \samp{POST} requests:
|
|---|
| 421 |
|
|---|
| 422 | \begin{verbatim}
|
|---|
| 423 | >>> import httplib, urllib
|
|---|
| 424 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
|
|---|
| 425 | >>> headers = {"Content-type": "application/x-www-form-urlencoded",
|
|---|
| 426 | ... "Accept": "text/plain"}
|
|---|
| 427 | >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
|
|---|
| 428 | >>> conn.request("POST", "/cgi-bin/query", params, headers)
|
|---|
| 429 | >>> response = conn.getresponse()
|
|---|
| 430 | >>> print response.status, response.reason
|
|---|
| 431 | 200 OK
|
|---|
| 432 | >>> data = response.read()
|
|---|
| 433 | >>> conn.close()
|
|---|
| 434 | \end{verbatim}
|
|---|