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 |
| 12 | that use HTTP and HTTPS. \strong{Note:} HTTPS support is only |
| 13 | available if the \refmodule{socket} module was compiled with SSL |
| 14 | support. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 15 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 16 | The module defines one class, \class{HTTP}: |
| 17 | |
| 18 | \begin{classdesc}{HTTP}{\optional{host\optional{, port}}} |
| 19 | An \class{HTTP} instance |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 20 | represents one transaction with an HTTP server. It should be |
| 21 | instantiated passing it a host and optional port number. If no port |
| 22 | number is passed, the port is extracted from the host string if it has |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 23 | the form \code{\var{host}:\var{port}}, else the default HTTP port (80) |
| 24 | is used. If no host is passed, no connection is made, and the |
| 25 | \method{connect()} method should be used to connect to a server. For |
| 26 | example, the following calls all create instances that connect to the |
| 27 | server at the same host and port: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 28 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 29 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 30 | >>> h1 = httplib.HTTP('www.cwi.nl') |
| 31 | >>> h2 = httplib.HTTP('www.cwi.nl:80') |
| 32 | >>> h3 = httplib.HTTP('www.cwi.nl', 80) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 33 | \end{verbatim} |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 34 | |
| 35 | Once an \class{HTTP} instance has been connected to an HTTP server, it |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 36 | should be used as follows: |
| 37 | |
| 38 | \begin{enumerate} |
| 39 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 40 | \item[1.] Make exactly one call to the \method{putrequest()} method. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 41 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 42 | \item[2.] Make zero or more calls to the \method{putheader()} method. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 43 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 44 | \item[3.] Call the \method{endheaders()} method (this can be omitted if |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 45 | step 4 makes no calls). |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 46 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 47 | \item[4.] Optional calls to the \method{send()} method. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 48 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 49 | \item[5.] Call the \method{getreply()} method. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 50 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 51 | \item[6.] Call the \method{getfile()} method and read the data off the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 52 | file object that it returns. |
| 53 | |
| 54 | \end{enumerate} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 55 | \end{classdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 57 | \subsection{HTTP Objects} |
| 58 | |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 59 | \class{HTTP} instances have the following methods: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 61 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 62 | \begin{methoddesc}{set_debuglevel}{level} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 63 | Set the debugging level (the amount of debugging output printed). |
| 64 | The default debug level is \code{0}, meaning no debugging output is |
| 65 | printed. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 66 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 67 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 68 | \begin{methoddesc}{connect}{host\optional{, port}} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 69 | Connect to the server given by \var{host} and \var{port}. See the |
| 70 | intro for the default port. This should be called directly only if |
| 71 | the instance was instantiated without passing a host. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 72 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 73 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 74 | \begin{methoddesc}{send}{data} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 75 | 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] | 76 | \method{endheaders()} method has been called and before |
| 77 | \method{getreply()} has been called. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 78 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 79 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 80 | \begin{methoddesc}{putrequest}{request, selector} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 81 | This should be the first call after the connection to the server has |
| 82 | been made. It sends a line to the server consisting of the |
| 83 | \var{request} string, the \var{selector} string, and the HTTP version |
| 84 | (\code{HTTP/1.0}). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 85 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 86 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 87 | \begin{methoddesc}{putheader}{header, argument\optional{, ...}} |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 88 | 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] | 89 | server consisting of the header, a colon and a space, and the first |
| 90 | argument. If more arguments are given, continuation lines are sent, |
| 91 | each consisting of a tab and an argument. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 92 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 93 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 94 | \begin{methoddesc}{endheaders}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 95 | 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] | 96 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 97 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 98 | \begin{methoddesc}{getreply}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 99 | Complete the request by shutting down the sending end of the socket, |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 100 | read the reply from the server, and return a triple |
| 101 | \code{(\var{replycode}, \var{message}, \var{headers})}. Here, |
Fred Drake | 1776556 | 1998-11-30 19:00:16 +0000 | [diff] [blame] | 102 | \var{replycode} is the integer reply code from the request (e.g., |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 103 | \code{200} if the request was handled properly); \var{message} is the |
| 104 | message string corresponding to the reply code; and \var{headers} is |
| 105 | an instance of the class \class{mimetools.Message} containing the |
| 106 | headers received from the server. See the description of the |
Fred Drake | 12a9569 | 1999-04-22 16:47:27 +0000 | [diff] [blame] | 107 | \refmodule{mimetools}\refstmodindex{mimetools} module. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 108 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 109 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 110 | \begin{methoddesc}{getfile}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 111 | Return a file object from which the data returned by the server can be |
Fred Drake | a2e9818 | 1998-03-12 05:54:02 +0000 | [diff] [blame] | 112 | read, using the \method{read()}, \method{readline()} or |
| 113 | \method{readlines()} methods. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 114 | \end{methoddesc} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 115 | |
Fred Drake | c0765c2 | 2001-09-25 16:32:02 +0000 | [diff] [blame] | 116 | |
Fred Drake | ef8cd7c | 2001-01-22 17:42:32 +0000 | [diff] [blame] | 117 | \subsection{Examples \label{httplib-examples}} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 118 | |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 119 | Here is an example session that uses the \samp{GET} method: |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 120 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 121 | \begin{verbatim} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 122 | >>> import httplib |
| 123 | >>> h = httplib.HTTP('www.cwi.nl') |
| 124 | >>> h.putrequest('GET', '/index.html') |
| 125 | >>> h.putheader('Accept', 'text/html') |
| 126 | >>> h.putheader('Accept', 'text/plain') |
Fred Drake | 481cf2c | 2001-09-01 02:35:23 +0000 | [diff] [blame] | 127 | >>> h.putheader('Host', 'www.cwi.nl') |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 128 | >>> h.endheaders() |
| 129 | >>> errcode, errmsg, headers = h.getreply() |
| 130 | >>> print errcode # Should be 200 |
| 131 | >>> f = h.getfile() |
Guido van Rossum | 240ddc8 | 1997-12-02 20:08:06 +0000 | [diff] [blame] | 132 | >>> data = f.read() # Get the raw HTML |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 133 | >>> f.close() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 134 | \end{verbatim} |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 135 | |
| 136 | Here is an example session that shows how to \samp{POST} requests: |
| 137 | |
| 138 | \begin{verbatim} |
| 139 | >>> import httplib, urllib |
| 140 | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) |
| 141 | >>> h = httplib.HTTP("www.musi-cal.com:80") |
| 142 | >>> h.putrequest("POST", "/cgi-bin/query") |
Fred Drake | d99e534 | 2000-11-17 18:04:03 +0000 | [diff] [blame] | 143 | >>> h.putheader("Content-type", "application/x-www-form-urlencoded") |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 144 | >>> h.putheader("Content-length", "%d" % len(params)) |
| 145 | >>> h.putheader('Accept', 'text/plain') |
| 146 | >>> h.putheader('Host', 'www.musi-cal.com') |
| 147 | >>> h.endheaders() |
Fred Drake | d99e534 | 2000-11-17 18:04:03 +0000 | [diff] [blame] | 148 | >>> h.send(params) |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 149 | >>> reply, msg, hdrs = h.getreply() |
Fred Drake | d99e534 | 2000-11-17 18:04:03 +0000 | [diff] [blame] | 150 | >>> print reply # should be 200 |
Fred Drake | 4e716fa | 2000-06-28 21:51:43 +0000 | [diff] [blame] | 151 | >>> data = h.getfile().read() # get the raw HTML |
| 152 | \end{verbatim} |