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