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