2007-05-08

Python behind proxy

Documentation on urllib2 is just a semblance.
So far the volume is too short, and the main thing is the working sample of how to download a file behind proxy using urllib2 in python.
Big Tnks to an unknown programmer

The code

import urllib2
# http://www.dbforums.com/archive/index.php/t-1419974.html
# by John J. Lee
class DumbProxyPasswordMgr:
def __init__(self):
self.user = self.passwd = None
def add_password(self, realm, uri, user, passwd):
self.user = user
self.passwd = passwd
def find_user_password(self, realm, authuri):
return self.user, self.passwd

url = "http://www.ya.ru"
proxy= urllib2.ProxyHandler({"http" : "http://proxy:8080"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(DumbProxyPasswordMgr ())
proxy_auth_handler.add_password(None, None, 'user', 'password')
opener = urllib2.build_opener(proxy,proxy_auth_handler)
urllib2.install_opener(opener)

src = urllib2.urlopen(url)
data = src.read()
dst = open("index.html","wb");
dst.write(data)

No comments:

Post a Comment