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