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