Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
be83737c
Kaydet (Commit)
be83737c
authored
Agu 25, 2004
tarafından
Martin v. Löwis
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Patch #798244: More urllib2 examples.
üst
c11d6f13
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
1 deletion
+62
-1
liburllib2.tex
Doc/lib/liburllib2.tex
+62
-1
No files found.
Doc/lib/liburllib2.tex
Dosyayı görüntüle @
be83737c
...
@@ -150,7 +150,7 @@ Cause requests to go through a proxy.
...
@@ -150,7 +150,7 @@ Cause requests to go through a proxy.
If
\var
{
proxies
}
is given, it must be a dictionary mapping
If
\var
{
proxies
}
is given, it must be a dictionary mapping
protocol names to URLs of proxies.
protocol names to URLs of proxies.
The default is to read the list of proxies from the environment
The default is to read the list of proxies from the environment
variables
\
var
{
protocol
}_
proxy
.
variables
\
envvar
{
<protocol>
_
proxy
}
.
\end{classdesc}
\end{classdesc}
\begin{classdesc}
{
HTTPPasswordMgr
}{}
\begin{classdesc}
{
HTTPPasswordMgr
}{}
...
@@ -790,3 +790,64 @@ import sys
...
@@ -790,3 +790,64 @@ import sys
data = sys.stdin.read()
data = sys.stdin.read()
print 'Content-type: text-plain
\n\nGot
Data: "
%s"' % data
print 'Content-type: text-plain
\n\nGot
Data: "
%s"' % data
\end{verbatim}
\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