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