annotate Doc/libhttplib.tex @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 7ef72873e4d6
children 6fda1e6e2616
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
1 \section{Standard Module \sectcode{httplib}}
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 \stmodindex{httplib}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 \index{HTTP}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2449
diff changeset
5 \renewcommand{\indexsubitem}{(in module httplib)}
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2449
diff changeset
6
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 This module defines a class which implements the client side of the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 HTTP protocol. It is normally not used directly --- the module
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
9 \code{urllib} uses it to handle URLs that use HTTP.
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 \stmodindex{urllib}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 The module defines one class, \code{HTTP}. An \code{HTTP} instance
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 represents one transaction with an HTTP server. It should be
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 instantiated passing it a host and optional port number. If no port
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 number is passed, the port is extracted from the host string if it has
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 the form \code{host:port}, else the default HTTP port (80) is used.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 If no host is passed, no connection is made, and the \code{connect}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
18 method should be used to connect to a server. For example, the
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
19 following calls all create instances that connect to the server at the
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
20 same host and port:
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
21
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
22 \begin{verbatim}
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
23 >>> h1 = httplib.HTTP('www.cwi.nl')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
24 >>> h2 = httplib.HTTP('www.cwi.nl:80')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
25 >>> h3 = httplib.HTTP('www.cwi.nl', 80)
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
26 \end{verbatim}
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 Once an \code{HTTP} instance has been connected to an HTTP server, it
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 should be used as follows:
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 \begin{enumerate}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 \item[1.] Make exactly one call to the \code{putrequest()} method.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 \item[2.] Make zero or more calls to the \code{putheader()} method.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 \item[3.] Call the \code{endheaders()} method (this can be omitted if
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
38 step 4 makes no calls).
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 \item[4.] Optional calls to the \code{send()} method.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 \item[5.] Call the \code{getreply()} method.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 \item[6.] Call the \code{getfile()} method and read the data off the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 file object that it returns.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 \end{enumerate}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
2609
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2581
diff changeset
49 \subsection{HTTP Objects}
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2581
diff changeset
50
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 \code{HTTP} instances have the following methods:
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
2609
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2581
diff changeset
53 \renewcommand{\indexsubitem}{(HTTP method)}
7ef72873e4d6 restructured library manual accordiung to functional group
Guido van Rossum <guido@python.org>
parents: 2581
diff changeset
54
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 \begin{funcdesc}{set_debuglevel}{level}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 Set the debugging level (the amount of debugging output printed).
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 The default debug level is \code{0}, meaning no debugging output is
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 printed.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 \begin{funcdesc}{connect}{host\optional{\, port}}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 Connect to the server given by \var{host} and \var{port}. See the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 intro for the default port. This should be called directly only if
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 the instance was instantiated without passing a host.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 \begin{funcdesc}{send}{data}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 Send data to the server. This should be used directly only after the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 \code{endheaders()} method has been called and before
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 \code{getreply()} has been called.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 \begin{funcdesc}{putrequest}{request\, selector}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 This should be the first call after the connection to the server has
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 been made. It sends a line to the server consisting of the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 \var{request} string, the \var{selector} string, and the HTTP version
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 (\code{HTTP/1.0}).
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 \begin{funcdesc}{putheader}{header\, argument\optional{\, ...}}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 Send an RFC-822 style header to the server. It sends a line to the
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 server consisting of the header, a colon and a space, and the first
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 argument. If more arguments are given, continuation lines are sent,
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 each consisting of a tab and an argument.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 \begin{funcdesc}{endheaders}{}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 Send a blank line to the server, signalling the end of the headers.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 \begin{funcdesc}{getreply}{}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 Complete the request by shutting down the sending end of the socket,
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 read the reply from the server, and return a triple (\var{replycode},
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 \var{message}, \var{headers}). Here \var{replycode} is the integer
2491
fb8e08ec88dd changes (suggested) by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
95 reply code from the request (e.g.\ \code{200} if the request was
2449
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 handled properly); \var{message} is the message string corresponding
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 to the reply code; and \var{header} is an instance of the class
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 \code{rfc822.Message} containing the headers received from the server.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 See the description of the \code{rfc822} module.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 \stmodindex{rfc822}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 \end{funcdesc}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 \begin{funcdesc}{getfile}{}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 Return a file object from which the data returned by the server can be
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 read, using the \code{read()}, \code{readline()} or \code{readlines()}
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 methods.
8eeecd233d56 added WWW sections
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 \end{funcdesc}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
108
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
109 \subsection{Example}
2581
4d28b655b728 added nodename macro calls for new very long section names
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
110 \nodename{HTTP Example}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
111
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
112 Here is an example session:
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
113
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
114 \begin{verbatim}
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
115 >>> import httplib
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
116 >>> h = httplib.HTTP('www.cwi.nl')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
117 >>> h.putrequest('GET', '/index.html')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
118 >>> h.putheader('Accept', 'text/html')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
119 >>> h.putheader('Accept', 'text/plain')
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
120 >>> h.endheaders()
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
121 >>> errcode, errmsg, headers = h.getreply()
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
122 >>> print errcode # Should be 200
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
123 >>> f = h.getfile()
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
124 >>> data f.read() # Get the raw HTML
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
125 >>> f.close()
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
126 >>>
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2491
diff changeset
127 \end{verbatim}