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 constants defined in this module are: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 28 | |
Fred Drake | 30bd666 | 2001-11-09 05:03:05 +0000 | [diff] [blame] | 29 | \begin{datadesc}{HTTP_PORT} |
| 30 | The default port for the HTTP protocol (always \code{80}). |
| 31 | \end{datadesc} |
| 32 | |
| 33 | \begin{datadesc}{HTTPS_PORT} |
| 34 | The default port for the HTTPS protocol (always \code{443}). |
| 35 | \end{datadesc} |
| 36 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 37 | The module provides the following classes: |
Fred Drake | 30bd666 | 2001-11-09 05:03:05 +0000 | [diff] [blame] | 38 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 39 | \begin{classdesc}{HTTPConnection}{host\optional{, port}} |
| 40 | An \class{HTTPConnection} instance represents one transaction with an HTTP |
| 41 | server. It should be instantiated passing it a host and optional port number. |
| 42 | If no port number is passed, the port is extracted from the host string if it |
| 43 | has the form \code{\var{host}:\var{port}}, else the default HTTP port (80) is |
| 44 | used. For example, the following calls all create instances that connect to |
| 45 | the server at the same host and port: |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 46 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 47 | \begin{verbatim} |
| 48 | >>> h1 = httplib.HTTPConnection('www.cwi.nl') |
| 49 | >>> h2 = httplib.HTTPConnection('www.cwi.nl:80') |
| 50 | >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80) |
| 51 | \end{verbatim} |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 52 | \versionadded{2.0} |
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 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 55 | \begin{classdesc}{HTTPSConnection}{host\optional{, port}} |
| 56 | A subclass of \class{HTTPConnection} that uses SSL for communication with |
| 57 | secure servers. Default port is \code{443}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 58 | \versionadded{2.0} |
| 59 | \end{classdesc} |
| 60 | |
| 61 | \begin{classdesc}{HTTPResponse}{sock\optional{, debuglevel=0}\optional{, strict=0}} |
| 62 | Class whose instances are returned upon successful connection. Not |
| 63 | instantiated directly by user. |
| 64 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 65 | \end{classdesc} |
| 66 | |
| 67 | The following exceptions are raised as appropriate: |
| 68 | |
| 69 | \begin{excdesc}{HTTPException} |
| 70 | The base class of the other exceptions in this module. It is a |
| 71 | subclass of \exception{Exception}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 72 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 73 | \end{excdesc} |
| 74 | |
| 75 | \begin{excdesc}{NotConnected} |
| 76 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 77 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 78 | \end{excdesc} |
| 79 | |
Skip Montanaro | 1e962cb | 2002-03-24 16:55:57 +0000 | [diff] [blame] | 80 | \begin{excdesc}{InvalidURL} |
| 81 | A subclass of \exception{HTTPException}, raised if a port is given and is |
| 82 | either non-numeric or empty. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 83 | \versionadded{2.3} |
Skip Montanaro | 1e962cb | 2002-03-24 16:55:57 +0000 | [diff] [blame] | 84 | \end{excdesc} |
| 85 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 86 | \begin{excdesc}{UnknownProtocol} |
| 87 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 88 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 89 | \end{excdesc} |
| 90 | |
| 91 | \begin{excdesc}{UnknownTransferEncoding} |
| 92 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 93 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 94 | \end{excdesc} |
| 95 | |
| 96 | \begin{excdesc}{UnimplementedFileMode} |
| 97 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 98 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 99 | \end{excdesc} |
| 100 | |
| 101 | \begin{excdesc}{IncompleteRead} |
| 102 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 103 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 104 | \end{excdesc} |
| 105 | |
| 106 | \begin{excdesc}{ImproperConnectionState} |
| 107 | A subclass of \exception{HTTPException}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 108 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 109 | \end{excdesc} |
| 110 | |
| 111 | \begin{excdesc}{CannotSendRequest} |
| 112 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 113 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 114 | \end{excdesc} |
| 115 | |
| 116 | \begin{excdesc}{CannotSendHeader} |
| 117 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 118 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 119 | \end{excdesc} |
| 120 | |
| 121 | \begin{excdesc}{ResponseNotReady} |
| 122 | A subclass of \exception{ImproperConnectionState}. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 123 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 124 | \end{excdesc} |
| 125 | |
| 126 | \begin{excdesc}{BadStatusLine} |
| 127 | A subclass of \exception{HTTPException}. Raised if a server responds with a |
| 128 | HTTP status code that we don't understand. |
Skip Montanaro | 13a2863 | 2003-01-27 15:00:38 +0000 | [diff] [blame] | 129 | \versionadded{2.0} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 130 | \end{excdesc} |
| 131 | |
| 132 | |
| 133 | \subsection{HTTPConnection Objects \label{httpconnection-objects}} |
| 134 | |
| 135 | \class{HTTPConnection} instances have the following methods: |
| 136 | |
| 137 | \begin{methoddesc}{request}{method, url\optional{, body\optional{, headers}}} |
| 138 | This will send a request to the server using the HTTP request method |
| 139 | \var{method} and the selector \var{url}. If the \var{body} argument is |
| 140 | present, it should be a string of data to send after the headers are finished. |
| 141 | The header Content-Length is automatically set to the correct value. |
| 142 | The \var{headers} argument should be a mapping of extra HTTP headers to send |
| 143 | with the request. |
| 144 | \end{methoddesc} |
| 145 | |
| 146 | \begin{methoddesc}{getresponse}{} |
| 147 | Should be called after a request is sent to get the response from the server. |
| 148 | Returns an \class{HTTPResponse} instance. |
| 149 | \end{methoddesc} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 150 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 151 | \begin{methoddesc}{set_debuglevel}{level} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 152 | Set the debugging level (the amount of debugging output printed). |
| 153 | The default debug level is \code{0}, meaning no debugging output is |
| 154 | printed. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 155 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 156 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 157 | \begin{methoddesc}{connect}{} |
| 158 | Connect to the server specified when the object was created. |
| 159 | \end{methoddesc} |
| 160 | |
| 161 | \begin{methoddesc}{close}{} |
| 162 | Close the connection to the server. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 163 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 164 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 165 | \begin{methoddesc}{send}{data} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 166 | Send data to the server. This should be used directly only after the |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 167 | \method{endheaders()} method has been called and before |
| 168 | \method{getreply()} has been called. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 169 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 170 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 171 | \begin{methoddesc}{putrequest}{request, selector} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 172 | This should be the first call after the connection to the server has |
| 173 | been made. It sends a line to the server consisting of the |
| 174 | \var{request} string, the \var{selector} string, and the HTTP version |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 175 | (\code{HTTP/1.1}). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 176 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 177 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 178 | \begin{methoddesc}{putheader}{header, argument\optional{, ...}} |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 179 | 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] | 180 | server consisting of the header, a colon and a space, and the first |
| 181 | argument. If more arguments are given, continuation lines are sent, |
| 182 | each consisting of a tab and an argument. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 183 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 184 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 185 | \begin{methoddesc}{endheaders}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 186 | 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] | 187 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 188 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 189 | |
| 190 | \subsection{HTTPResponse Objects \label{httpresponse-objects}} |
| 191 | |
| 192 | \class{HTTPResponse} instances have the following methods and attributes: |
| 193 | |
| 194 | \begin{methoddesc}{read}{} |
| 195 | Reads and returns the response body. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 196 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 197 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 198 | \begin{methoddesc}{getheader}{name\optional{, default}} |
| 199 | Get the contents of the header \var{name}, or \var{default} if there is no |
| 200 | matching header. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 201 | \end{methoddesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 202 | |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 203 | \begin{datadesc}{msg} |
| 204 | A \class{mimetools.Message} instance containing the response headers. |
| 205 | \end{datadesc} |
| 206 | |
| 207 | \begin{datadesc}{version} |
| 208 | HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1. |
| 209 | \end{datadesc} |
| 210 | |
| 211 | \begin{datadesc}{status} |
| 212 | Status code returned by server. |
| 213 | \end{datadesc} |
| 214 | |
| 215 | \begin{datadesc}{reason} |
| 216 | Reason phrase returned by server. |
| 217 | \end{datadesc} |
| 218 | |
Fred Drake | c0765c2 | 2001-09-25 16:32:02 +0000 | [diff] [blame] | 219 | |
Fred Drake | ef8cd7c | 2001-01-22 17:42:32 +0000 | [diff] [blame] | 220 | \subsection{Examples \label{httplib-examples}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 221 | |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 222 | Here is an example session that uses the \samp{GET} method: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 223 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 224 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 225 | >>> import httplib |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 226 | >>> conn = httplib.HTTPConnection("www.python.org") |
| 227 | >>> conn.request("GET", "/index.html") |
| 228 | >>> r1 = conn.getresponse() |
| 229 | >>> print r1.status, r1.reason |
| 230 | 200 OK |
| 231 | >>> data1 = r1.read() |
| 232 | >>> conn.request("GET", "/parrot.spam") |
| 233 | >>> r2 = conn.getresponse() |
| 234 | >>> print r2.status, r2.reason |
| 235 | 404 Not Found |
| 236 | >>> data2 = r2.read() |
| 237 | >>> conn.close() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 238 | \end{verbatim} |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 239 | |
| 240 | Here is an example session that shows how to \samp{POST} requests: |
| 241 | |
| 242 | \begin{verbatim} |
| 243 | >>> import httplib, urllib |
| 244 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 245 | >>> headers = {"Content-type": "application/x-www-form-urlencoded", |
| 246 | ... "Accept": "text/plain"} |
| 247 | >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") |
| 248 | >>> conn.request("POST", "/cgi-bin/query", params, headers) |
Fred Drake | dce2e11 | 2001-12-21 03:52:04 +0000 | [diff] [blame] | 249 | >>> response = conn.getresponse() |
Fred Drake | 38f3b72 | 2001-11-30 06:06:40 +0000 | [diff] [blame] | 250 | >>> print response.status, response.reason |
| 251 | 200 OK |
| 252 | >>> data = response.read() |
| 253 | >>> conn.close() |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 254 | \end{verbatim} |