Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{httplib} --- |
Fred Drake | 12a9569 | 1999-04-22 16:47:27 +0000 | [diff] [blame] | 2 | HTTP protocol client} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 12a9569 | 1999-04-22 16:47:27 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{httplib} |
Fred Drake | c0765c2 | 2001-09-25 16:32:02 +0000 | [diff] [blame] | 5 | \modulesynopsis{HTTP and HTTPS protocol client (requires sockets).} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 7 | \indexii{HTTP}{protocol} |
Fred Drake | ef338ec | 2001-12-26 19:48:43 +0000 | [diff] [blame] | 8 | \index{HTTP!\module{httplib} (standard module)} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 9 | |
Fred Drake | c0765c2 | 2001-09-25 16:32:02 +0000 | [diff] [blame] | 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 |
Fred Drake | 0a9cc58 | 2003-01-27 16:32:04 +0000 | [diff] [blame] | 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} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 26 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 27 | The module provides the following classes: |
Fred Drake | 30bd666 | 2001-11-09 05:03:05 +0000 | [diff] [blame] | 28 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 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: |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 36 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 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} |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 42 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 43 | \end{classdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 235d1ef | 2003-05-20 02:56:35 +0000 | [diff] [blame] | 45 | \begin{classdesc}{HTTPSConnection}{host\optional{, port, key_file, cert_file}} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 46 | A subclass of \class{HTTPConnection} that uses SSL for communication with |
| 47 | secure servers. Default port is \code{443}. |
Brett Cannon | 235d1ef | 2003-05-20 02:56:35 +0000 | [diff] [blame] | 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 | |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 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} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 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}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 68 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 69 | \end{excdesc} |
| 70 | |
| 71 | \begin{excdesc}{NotConnected} |
| 72 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 73 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 74 | \end{excdesc} |
| 75 | |
Skip Montanaro | 1e962cb | 2002-03-24 16:55:57 +0000 | [diff] [blame] | 76 | \begin{excdesc}{InvalidURL} |
| 77 | A subclass of \exception{HTTPException}, raised if a port is given and is |
| 78 | either non-numeric or empty. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 79 | \versionadded{2.3} |
Skip Montanaro | 1e962cb | 2002-03-24 16:55:57 +0000 | [diff] [blame] | 80 | \end{excdesc} |
| 81 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 82 | \begin{excdesc}{UnknownProtocol} |
| 83 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 84 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 85 | \end{excdesc} |
| 86 | |
| 87 | \begin{excdesc}{UnknownTransferEncoding} |
| 88 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 89 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 90 | \end{excdesc} |
| 91 | |
| 92 | \begin{excdesc}{UnimplementedFileMode} |
| 93 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 94 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 95 | \end{excdesc} |
| 96 | |
| 97 | \begin{excdesc}{IncompleteRead} |
| 98 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 99 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 100 | \end{excdesc} |
| 101 | |
| 102 | \begin{excdesc}{ImproperConnectionState} |
| 103 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 104 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 105 | \end{excdesc} |
| 106 | |
| 107 | \begin{excdesc}{CannotSendRequest} |
| 108 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 109 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 110 | \end{excdesc} |
| 111 | |
| 112 | \begin{excdesc}{CannotSendHeader} |
| 113 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 114 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 115 | \end{excdesc} |
| 116 | |
| 117 | \begin{excdesc}{ResponseNotReady} |
| 118 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 119 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 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. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 125 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 126 | \end{excdesc} |
| 127 | |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 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} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 149 | {http://www.webdav.org/specs/rfc2518.html#STATUS_102}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 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} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 174 | {http://www.webdav.org/specs/rfc2518.html#STATUS_207}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 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} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 256 | {http://www.webdav.org/specs/rfc2518.html#STATUS_422}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 257 | \lineiii{LOCKED}{\code{423}} |
| 258 | {WEBDAV \ulink{RFC 2518, Section 10.4} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 259 | {http://www.webdav.org/specs/rfc2518.html#STATUS_423}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 260 | \lineiii{FAILED_DEPENDENCY}{\code{424}} |
| 261 | {WEBDAV, \ulink{RFC 2518, Section 10.5} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 262 | {http://www.webdav.org/specs/rfc2518.html#STATUS_424}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 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} |
Georg Brandl | 7f26a62 | 2005-08-27 17:04:58 +0000 | [diff] [blame^] | 286 | {http://www.webdav.org/specs/rfc2518.html#STATUS_507}} |
Martin v. Löwis | 39a3178 | 2004-09-18 09:03:49 +0000 | [diff] [blame] | 287 | \lineiii{NOT_EXTENDED}{\code{510}} |
| 288 | {An HTTP Extension Framework, \rfc{2774}, Section 7} |
| 289 | \end{tableiii} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 290 | |
| 291 | \subsection{HTTPConnection Objects \label{httpconnection-objects}} |
| 292 | |
| 293 | \class{HTTPConnection} instances have the following methods: |
| 294 | |
| 295 | \begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}} |
| 296 | This will send a request to the server using the HTTP request method |
| 297 | \var{method} and the selector \var{url}. If the \var{body} argument is |
| 298 | present, it should be a string of data to send after the headers are finished. |
| 299 | The header Content-Length is automatically set to the correct value. |
| 300 | The \var{headers} argument should be a mapping of extra HTTP headers to send |
| 301 | with the request. |
| 302 | \end{methoddesc} |
| 303 | |
| 304 | \begin{methoddesc}{getresponse}{} |
| 305 | Should be called after a request is sent to get the response from the server. |
| 306 | Returns an \class{HTTPResponse} instance. |
Georg Brandl | 71de040 | 2005-06-25 19:15:48 +0000 | [diff] [blame] | 307 | \note{Note that you must have read the whole response before you can send a new |
| 308 | request to the server.} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 309 | \end{methoddesc} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 310 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 311 | \begin{methoddesc}{set_debuglevel}{level} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 312 | Set the debugging level (the amount of debugging output printed). |
| 313 | The default debug level is \code{0}, meaning no debugging output is |
| 314 | printed. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 315 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 316 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 317 | \begin{methoddesc}{connect}{} |
| 318 | Connect to the server specified when the object was created. |
| 319 | \end{methoddesc} |
| 320 | |
| 321 | \begin{methoddesc}{close}{} |
| 322 | Close the connection to the server. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 323 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 324 | |
Georg Brandl | 71de040 | 2005-06-25 19:15:48 +0000 | [diff] [blame] | 325 | As an alternative to using the \method{request()} method described above, |
| 326 | you can also send your request step by step, by using the four functions |
| 327 | below. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 328 | |
Martin v. Löwis | af7dc8d | 2003-11-19 19:51:55 +0000 | [diff] [blame] | 329 | \begin{methoddesc}{putrequest}{request, selector\optional{, |
| 330 | skip\_host\optional{, skip_accept_encoding}}} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 331 | This should be the first call after the connection to the server has |
| 332 | been made. It sends a line to the server consisting of the |
| 333 | \var{request} string, the \var{selector} string, and the HTTP version |
Martin v. Löwis | af7dc8d | 2003-11-19 19:51:55 +0000 | [diff] [blame] | 334 | (\code{HTTP/1.1}). To disable automatic sending of \code{Host:} or |
| 335 | \code{Accept-Encoding:} headers (for example to accept additional |
| 336 | content encodings), specify \var{skip_host} or \var{skip_accept_encoding} |
| 337 | with non-False values. |
| 338 | \versionchanged[\var{skip_accept_encoding} argument added]{2.4} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 339 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 340 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 341 | \begin{methoddesc}{putheader}{header, argument\optional{, ...}} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 342 | Send an \rfc{822}-style header to the server. It sends a line to the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 343 | server consisting of the header, a colon and a space, and the first |
| 344 | argument. If more arguments are given, continuation lines are sent, |
| 345 | each consisting of a tab and an argument. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 346 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 347 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 348 | \begin{methoddesc}{endheaders}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 349 | Send a blank line to the server, signalling the end of the headers. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 350 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 351 | |
Georg Brandl | 71de040 | 2005-06-25 19:15:48 +0000 | [diff] [blame] | 352 | \begin{methoddesc}{send}{data} |
| 353 | Send data to the server. This should be used directly only after the |
| 354 | \method{endheaders()} method has been called and before |
| 355 | \method{getresponse()} is called. |
| 356 | \end{methoddesc} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 357 | |
| 358 | \subsection{HTTPResponse Objects \label{httpresponse-objects}} |
| 359 | |
| 360 | \class{HTTPResponse} instances have the following methods and attributes: |
| 361 | |
Raymond Hettinger | 09c7b60 | 2003-09-02 02:32:54 +0000 | [diff] [blame] | 362 | \begin{methoddesc}{read}{\optional{amt}} |
| 363 | Reads and returns the response body, or up to the next \var{amt} bytes. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 364 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 365 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 366 | \begin{methoddesc}{getheader}{name\optional{, default}} |
| 367 | Get the contents of the header \var{name}, or \var{default} if there is no |
| 368 | matching header. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 369 | \end{methoddesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 370 | |
Martin v. Löwis | deacce2 | 2004-08-18 12:46:26 +0000 | [diff] [blame] | 371 | \begin{methoddesc}{getheaders}{} |
| 372 | Return a list of (header, value) tuples. \versionadded{2.4} |
| 373 | \end{methoddesc} |
| 374 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 375 | \begin{datadesc}{msg} |
| 376 | A \class{mimetools.Message} instance containing the response headers. |
| 377 | \end{datadesc} |
| 378 | |
| 379 | \begin{datadesc}{version} |
| 380 | HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1. |
| 381 | \end{datadesc} |
| 382 | |
| 383 | \begin{datadesc}{status} |
| 384 | Status code returned by server. |
| 385 | \end{datadesc} |
| 386 | |
| 387 | \begin{datadesc}{reason} |
| 388 | Reason phrase returned by server. |
| 389 | \end{datadesc} |
| 390 | |
Fred Drake | c0765c2 | 2001-09-25 16:32:02 +0000 | [diff] [blame] | 391 | |
Fred Drake | ef8cd7c | 2001-01-22 17:42:32 +0000 | [diff] [blame] | 392 | \subsection{Examples \label{httplib-examples}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 393 | |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 394 | Here is an example session that uses the \samp{GET} method: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 395 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 396 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 397 | >>> import httplib |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 398 | >>> conn = httplib.HTTPConnection("www.python.org") |
| 399 | >>> conn.request("GET", "/index.html") |
| 400 | >>> r1 = conn.getresponse() |
| 401 | >>> print r1.status, r1.reason |
| 402 | 200 OK |
| 403 | >>> data1 = r1.read() |
| 404 | >>> conn.request("GET", "/parrot.spam") |
| 405 | >>> r2 = conn.getresponse() |
| 406 | >>> print r2.status, r2.reason |
| 407 | 404 Not Found |
| 408 | >>> data2 = r2.read() |
| 409 | >>> conn.close() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 410 | \end{verbatim} |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 411 | |
| 412 | Here is an example session that shows how to \samp{POST} requests: |
| 413 | |
| 414 | \begin{verbatim} |
| 415 | >>> import httplib, urllib |
| 416 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 417 | >>> headers = {"Content-type": "application/x-www-form-urlencoded", |
| 418 | ... "Accept": "text/plain"} |
| 419 | >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") |
| 420 | >>> conn.request("POST", "/cgi-bin/query", params, headers) |
Fred Drake | dce2e11 | 2001-12-21 03:52:04 +0000 | [diff] [blame] | 421 | >>> response = conn.getresponse() |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 422 | >>> print response.status, response.reason |
| 423 | 200 OK |
| 424 | >>> data = response.read() |
| 425 | >>> conn.close() |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 426 | \end{verbatim} |