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