| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{httplib} --- | 
					
						
							| 
									
										
										
										
											1999-04-22 16:47:27 +00:00
										 |  |  |          HTTP protocol client} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 16:47:27 +00:00
										 |  |  | \declaremodule{standard}{httplib} | 
					
						
							| 
									
										
										
										
											2001-09-25 16:32:02 +00:00
										 |  |  | \modulesynopsis{HTTP and HTTPS protocol client (requires sockets).} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \indexii{HTTP}{protocol} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-25 16:32:02 +00:00
										 |  |  | This module defines classes which implement the client side of the | 
					
						
							|  |  |  | HTTP and HTTPS protocols.  It is normally not used directly --- the | 
					
						
							|  |  |  | module \refmodule{urllib}\refstmodindex{urllib} uses it to handle URLs | 
					
						
							|  |  |  | that use HTTP and HTTPS.  \strong{Note:}  HTTPS support is only | 
					
						
							|  |  |  | available if the \refmodule{socket} module was compiled with SSL | 
					
						
							|  |  |  | support. | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | The module defines one class, \class{HTTP}: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{classdesc}{HTTP}{\optional{host\optional{, port}}} | 
					
						
							|  |  |  | An \class{HTTP} instance | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | represents one transaction with an HTTP server.  It should be | 
					
						
							|  |  |  | instantiated passing it a host and optional port number.  If no port | 
					
						
							|  |  |  | number is passed, the port is extracted from the host string if it has | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | the form \code{\var{host}:\var{port}}, else the default HTTP port (80) | 
					
						
							|  |  |  | is used.  If no host is passed, no connection is made, and the | 
					
						
							|  |  |  | \method{connect()} method should be used to connect to a server.  For | 
					
						
							|  |  |  | example, the following calls all create instances that connect to the | 
					
						
							|  |  |  | server at the same host and port: | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | >>> h1 = httplib.HTTP('www.cwi.nl') | 
					
						
							|  |  |  | >>> h2 = httplib.HTTP('www.cwi.nl:80') | 
					
						
							|  |  |  | >>> h3 = httplib.HTTP('www.cwi.nl', 80) | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Once an \class{HTTP} instance has been connected to an HTTP server, it | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | should be used as follows: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{enumerate} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[1.] Make exactly one call to the \method{putrequest()} method. | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[2.] Make zero or more calls to the \method{putheader()} method. | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[3.] Call the \method{endheaders()} method (this can be omitted if | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | step 4 makes no calls). | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[4.] Optional calls to the \method{send()} method. | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[5.] Call the \method{getreply()} method. | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \item[6.] Call the \method{getfile()} method and read the data off the | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | file object that it returns. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \end{enumerate} | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{classdesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-28 13:35:14 +00:00
										 |  |  | \subsection{HTTP Objects} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \class{HTTP} instances have the following methods: | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-03-28 13:35:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{set_debuglevel}{level} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Set the debugging level (the amount of debugging output printed). | 
					
						
							|  |  |  | The default debug level is \code{0}, meaning no debugging output is | 
					
						
							|  |  |  | printed. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{connect}{host\optional{, port}} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Connect to the server given by \var{host} and \var{port}.  See the | 
					
						
							|  |  |  | intro for the default port.  This should be called directly only if | 
					
						
							|  |  |  | the instance was instantiated without passing a host. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{send}{data} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Send data to the server.  This should be used directly only after the | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \method{endheaders()} method has been called and before | 
					
						
							|  |  |  | \method{getreply()} has been called. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{putrequest}{request, selector} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | This should be the first call after the connection to the server has | 
					
						
							|  |  |  | been made.  It sends a line to the server consisting of the | 
					
						
							|  |  |  | \var{request} string, the \var{selector} string, and the HTTP version | 
					
						
							|  |  |  | (\code{HTTP/1.0}). | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{putheader}{header, argument\optional{, ...}} | 
					
						
							| 
									
										
										
										
											1998-02-09 19:16:20 +00:00
										 |  |  | Send an \rfc{822} style header to the server.  It sends a line to the | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | server consisting of the header, a colon and a space, and the first | 
					
						
							|  |  |  | argument.  If more arguments are given, continuation lines are sent, | 
					
						
							|  |  |  | each consisting of a tab and an argument. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{endheaders}{} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Send a blank line to the server, signalling the end of the headers. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{getreply}{} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Complete the request by shutting down the sending end of the socket, | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | read the reply from the server, and return a triple | 
					
						
							|  |  |  | \code{(\var{replycode}, \var{message}, \var{headers})}.  Here, | 
					
						
							| 
									
										
										
										
											1998-11-30 19:00:16 +00:00
										 |  |  | \var{replycode} is the integer reply code from the request (e.g., | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | \code{200} if the request was handled properly); \var{message} is the | 
					
						
							|  |  |  | message string corresponding to the reply code; and \var{headers} is | 
					
						
							|  |  |  | an instance of the class \class{mimetools.Message} containing the | 
					
						
							|  |  |  | headers received from the server.  See the description of the | 
					
						
							| 
									
										
										
										
											1999-04-22 16:47:27 +00:00
										 |  |  | \refmodule{mimetools}\refstmodindex{mimetools} module. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \begin{methoddesc}{getfile}{} | 
					
						
							| 
									
										
										
										
											1995-02-27 17:53:25 +00:00
										 |  |  | Return a file object from which the data returned by the server can be | 
					
						
							| 
									
										
										
										
											1998-03-12 05:54:02 +00:00
										 |  |  | read, using the \method{read()}, \method{readline()} or | 
					
						
							|  |  |  | \method{readlines()} methods. | 
					
						
							| 
									
										
										
										
											1998-04-04 07:15:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-25 16:32:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-22 17:42:32 +00:00
										 |  |  | \subsection{Examples \label{httplib-examples}} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-28 21:51:43 +00:00
										 |  |  | Here is an example session that uses the \samp{GET} method: | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | >>> import httplib | 
					
						
							|  |  |  | >>> h = httplib.HTTP('www.cwi.nl') | 
					
						
							|  |  |  | >>> h.putrequest('GET', '/index.html') | 
					
						
							|  |  |  | >>> h.putheader('Accept', 'text/html') | 
					
						
							|  |  |  | >>> h.putheader('Accept', 'text/plain') | 
					
						
							| 
									
										
										
										
											2001-09-01 02:35:23 +00:00
										 |  |  | >>> h.putheader('Host', 'www.cwi.nl') | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | >>> h.endheaders() | 
					
						
							|  |  |  | >>> errcode, errmsg, headers = h.getreply() | 
					
						
							|  |  |  | >>> print errcode # Should be 200 | 
					
						
							|  |  |  | >>> f = h.getfile() | 
					
						
							| 
									
										
										
										
											1997-12-02 20:08:06 +00:00
										 |  |  | >>> data = f.read() # Get the raw HTML | 
					
						
							| 
									
										
										
										
											1995-03-17 16:07:09 +00:00
										 |  |  | >>> f.close() | 
					
						
							| 
									
										
										
										
											1998-02-13 06:58:54 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											2000-06-28 21:51:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Here is an example session that shows how to \samp{POST} requests: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> import httplib, urllib | 
					
						
							|  |  |  | >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) | 
					
						
							|  |  |  | >>> h = httplib.HTTP("www.musi-cal.com:80") | 
					
						
							|  |  |  | >>> h.putrequest("POST", "/cgi-bin/query") | 
					
						
							| 
									
										
										
										
											2000-11-17 18:04:03 +00:00
										 |  |  | >>> h.putheader("Content-type", "application/x-www-form-urlencoded") | 
					
						
							| 
									
										
										
										
											2000-06-28 21:51:43 +00:00
										 |  |  | >>> h.putheader("Content-length", "%d" % len(params))
 | 
					
						
							|  |  |  | >>> h.putheader('Accept', 'text/plain') | 
					
						
							|  |  |  | >>> h.putheader('Host', 'www.musi-cal.com') | 
					
						
							|  |  |  | >>> h.endheaders() | 
					
						
							| 
									
										
										
										
											2000-11-17 18:04:03 +00:00
										 |  |  | >>> h.send(params) | 
					
						
							| 
									
										
										
										
											2000-06-28 21:51:43 +00:00
										 |  |  | >>> reply, msg, hdrs = h.getreply() | 
					
						
							| 
									
										
										
										
											2000-11-17 18:04:03 +00:00
										 |  |  | >>> print reply # should be 200 | 
					
						
							| 
									
										
										
										
											2000-06-28 21:51:43 +00:00
										 |  |  | >>> data = h.getfile().read() # get the raw HTML | 
					
						
							|  |  |  | \end{verbatim} |