Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
be83737c
Commit
be83737c
authored
Aug 25, 2004
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #798244: More urllib2 examples.
parent
c11d6f13
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
1 deletion
+62
-1
Doc/lib/liburllib2.tex
Doc/lib/liburllib2.tex
+62
-1
No files found.
Doc/lib/liburllib2.tex
View file @
be83737c
...
...
@@ -150,7 +150,7 @@ Cause requests to go through a proxy.
If
\var
{
proxies
}
is given, it must be a dictionary mapping
protocol names to URLs of proxies.
The default is to read the list of proxies from the environment
variables
\
var
{
protocol
}_
proxy
.
variables
\
envvar
{
<protocol>
_
proxy
}
.
\end{classdesc}
\begin{classdesc}
{
HTTPPasswordMgr
}{}
...
...
@@ -790,3 +790,64 @@ import sys
data = sys.stdin.read()
print 'Content-type: text-plain
\n\nGot
Data: "
%s"' % data
\end{verbatim}
Use of Basic HTTP Authentication:
\begin{verbatim}
import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth
_
handler = urllib2.HTTPBasicAuthHandler()
auth
_
handler.add
_
password('realm', 'host', 'username', 'password')
opener = urllib2.build
_
opener(auth
_
handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install
_
opener(opener)
urllib2.urlopen('http://www.example.com/login.html')
\end{verbatim}
\function
{
build
_
opener()
}
provides many handlers by default, including a
\class
{
ProxyHandler
}
. By default,
\class
{
ProxyHandler
}
uses the
environment variables named
\code
{
<scheme>
_
proxy
}
, where
\code
{
<scheme>
}
is the URL scheme involved. For example, the
\envvar
{
http
_
proxy
}
environment variable is read to obtain the HTTP proxy's URL.
This example replaces the default
\class
{
ProxyHandler
}
with one that uses
programatically-supplied proxy URLs, and adds proxy authorization support
with
\class
{
ProxyBasicAuthHandler
}
.
\begin{verbatim}
proxy
_
handler = urllib2.ProxyHandler(
{
'http': 'http://www.example.com:3128/'
}
)
proxy
_
auth
_
handler = urllib2.HTTPBasicAuthHandler()
proxy
_
auth
_
handler.add
_
password('realm', 'host', 'username', 'password')
opener = build
_
opener(proxy
_
handler, proxy
_
auth
_
handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')
\end{verbatim}
Adding HTTP headers:
Use the
\var
{
headers
}
argument to the
\class
{
Request
}
constructor, or:
\begin{verbatim}
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add
_
header('Referer', 'http://www.python.org/')
r = urllib2.urlopen(req)
\end{verbatim}
\class
{
OpenerDirector
}
automatically adds a
\mailheader
{
User-Agent
}
header to every
\class
{
Request
}
. To change this:
\begin{verbatim}
import urllib2
opener = urllib2.build
_
opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')
\end{verbatim}
Also, remember that a few standard headers
(
\mailheader
{
Content-Length
}
,
\mailheader
{
Content-Type
}
and
\mailheader
{
Host
}
) are added when the
\class
{
Request
}
is passed to
\function
{
urlopen()
}
(or
\method
{
OpenerDirector.open()
}
).
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment