Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="http-error-processor-objects.html" /> | |
13 | <link rel="parent" href="module-urllib2.html" /> | |
14 | <link rel="next" href="module-httplib.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>11.5.22 Examples </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="11.5.21 HTTPErrorProcessor Objects" | |
24 | href="http-error-processor-objects.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="11.5 urllib2 " | |
27 | href="module-urllib2.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="11.6 httplib " | |
30 | href="module-httplib.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="http-error-processor-objects.html">11.5.21 HTTPErrorProcessor Objects</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-urllib2.html">11.5 urllib2 </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-httplib.html">11.6 httplib </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION00135220000000000000000"></A><A NAME="urllib2-examples"></A> | |
55 | <BR> | |
56 | 11.5.22 Examples | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | This example gets the python.org main page and displays the first 100 | |
61 | bytes of it: | |
62 | ||
63 | <P> | |
64 | <div class="verbatim"><pre> | |
65 | >>> import urllib2 | |
66 | >>> f = urllib2.urlopen('http://www.python.org/') | |
67 | >>> print f.read(100) | |
68 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
69 | <?xml-stylesheet href="./css/ht2html | |
70 | </pre></div> | |
71 | ||
72 | <P> | |
73 | Here we are sending a data-stream to the stdin of a CGI and reading | |
74 | the data it returns to us: | |
75 | ||
76 | <P> | |
77 | <div class="verbatim"><pre> | |
78 | >>> import urllib2 | |
79 | >>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi', | |
80 | ... data='This data is passed to stdin of the CGI') | |
81 | >>> f = urllib2.urlopen(req) | |
82 | >>> print f.read() | |
83 | Got Data: "This data is passed to stdin of the CGI" | |
84 | </pre></div> | |
85 | ||
86 | <P> | |
87 | The code for the sample CGI used in the above example is: | |
88 | ||
89 | <P> | |
90 | <div class="verbatim"><pre> | |
91 | #!/usr/bin/env python | |
92 | import sys | |
93 | data = sys.stdin.read() | |
94 | print 'Content-type: text-plain\n\nGot Data: "%s"' % data | |
95 | </pre></div> | |
96 | ||
97 | <P> | |
98 | Use of Basic HTTP Authentication: | |
99 | ||
100 | <P> | |
101 | <div class="verbatim"><pre> | |
102 | import urllib2 | |
103 | # Create an OpenerDirector with support for Basic HTTP Authentication... | |
104 | auth_handler = urllib2.HTTPBasicAuthHandler() | |
105 | auth_handler.add_password('realm', 'host', 'username', 'password') | |
106 | opener = urllib2.build_opener(auth_handler) | |
107 | # ...and install it globally so it can be used with urlopen. | |
108 | urllib2.install_opener(opener) | |
109 | urllib2.urlopen('http://www.example.com/login.html') | |
110 | </pre></div> | |
111 | ||
112 | <P> | |
113 | <tt class="function">build_opener()</tt> provides many handlers by default, including a | |
114 | <tt class="class">ProxyHandler</tt>. By default, <tt class="class">ProxyHandler</tt> uses the | |
115 | environment variables named <code><scheme>_proxy</code>, where <code><scheme></code> | |
116 | is the URL scheme involved. For example, the <a class="envvar" id='l2h-3294' xml:id='l2h-3294'>http_proxy</a> | |
117 | environment variable is read to obtain the HTTP proxy's URL. | |
118 | ||
119 | <P> | |
120 | This example replaces the default <tt class="class">ProxyHandler</tt> with one that uses | |
121 | programatically-supplied proxy URLs, and adds proxy authorization support | |
122 | with <tt class="class">ProxyBasicAuthHandler</tt>. | |
123 | ||
124 | <P> | |
125 | <div class="verbatim"><pre> | |
126 | proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'}) | |
127 | proxy_auth_handler = urllib2.HTTPBasicAuthHandler() | |
128 | proxy_auth_handler.add_password('realm', 'host', 'username', 'password') | |
129 | ||
130 | opener = build_opener(proxy_handler, proxy_auth_handler) | |
131 | # This time, rather than install the OpenerDirector, we use it directly: | |
132 | opener.open('http://www.example.com/login.html') | |
133 | </pre></div> | |
134 | ||
135 | <P> | |
136 | Adding HTTP headers: | |
137 | ||
138 | <P> | |
139 | Use the <var>headers</var> argument to the <tt class="class">Request</tt> constructor, or: | |
140 | ||
141 | <P> | |
142 | <div class="verbatim"><pre> | |
143 | import urllib2 | |
144 | req = urllib2.Request('http://www.example.com/') | |
145 | req.add_header('Referer', 'http://www.python.org/') | |
146 | r = urllib2.urlopen(req) | |
147 | </pre></div> | |
148 | ||
149 | <P> | |
150 | <tt class="class">OpenerDirector</tt> automatically adds a <span class="mailheader">User-Agent:</span> | |
151 | header to every <tt class="class">Request</tt>. To change this: | |
152 | ||
153 | <P> | |
154 | <div class="verbatim"><pre> | |
155 | import urllib2 | |
156 | opener = urllib2.build_opener() | |
157 | opener.addheaders = [('User-agent', 'Mozilla/5.0')] | |
158 | opener.open('http://www.example.com/') | |
159 | </pre></div> | |
160 | ||
161 | <P> | |
162 | Also, remember that a few standard headers | |
163 | (<span class="mailheader">Content-Length:</span>, <span class="mailheader">Content-Type:</span> and | |
164 | <span class="mailheader">Host:</span>) are added when the <tt class="class">Request</tt> is passed to | |
165 | <tt class="function">urlopen()</tt> (or <tt class="method">OpenerDirector.open()</tt>). | |
166 | ||
167 | <DIV CLASS="navigation"> | |
168 | <div class='online-navigation'> | |
169 | <p></p><hr /> | |
170 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
171 | <tr> | |
172 | <td class='online-navigation'><a rel="prev" title="11.5.21 HTTPErrorProcessor Objects" | |
173 | href="http-error-processor-objects.html"><img src='../icons/previous.png' | |
174 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
175 | <td class='online-navigation'><a rel="parent" title="11.5 urllib2 " | |
176 | href="module-urllib2.html"><img src='../icons/up.png' | |
177 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
178 | <td class='online-navigation'><a rel="next" title="11.6 httplib " | |
179 | href="module-httplib.html"><img src='../icons/next.png' | |
180 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
181 | <td align="center" width="100%">Python Library Reference</td> | |
182 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
183 | href="contents.html"><img src='../icons/contents.png' | |
184 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
185 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
186 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
187 | <td class='online-navigation'><a rel="index" title="Index" | |
188 | href="genindex.html"><img src='../icons/index.png' | |
189 | border='0' height='32' alt='Index' width='32' /></A></td> | |
190 | </tr></table> | |
191 | <div class='online-navigation'> | |
192 | <b class="navlabel">Previous:</b> | |
193 | <a class="sectref" rel="prev" href="http-error-processor-objects.html">11.5.21 HTTPErrorProcessor Objects</A> | |
194 | <b class="navlabel">Up:</b> | |
195 | <a class="sectref" rel="parent" href="module-urllib2.html">11.5 urllib2 </A> | |
196 | <b class="navlabel">Next:</b> | |
197 | <a class="sectref" rel="next" href="module-httplib.html">11.6 httplib </A> | |
198 | </div> | |
199 | </div> | |
200 | <hr /> | |
201 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
202 | </DIV> | |
203 | <!--End of Navigation Panel--> | |
204 | <ADDRESS> | |
205 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
206 | </ADDRESS> | |
207 | </BODY> | |
208 | </HTML> |