views:

521

answers:

2

I open the urls with

site = urllib2.urlopen('http://google.com')

And what I wanna do is connect the same way with a proxy I got somwhere telling me

site = urllib2.urlopen('http://google.com', proxies={'http':'127.0.0.1'})

but that hadent worked either

I know urllib2 has something like a proxy handler but I cant recall that function

+2  A: 

You have to install a ProxyHandler

urllib2.install_opener(
    urllib2.build_opener(
        urllib2.ProxyHandler({'http': '127.0.0.1'})
    )
)
urllib2.urlopen('http://www.google.com')
dcrosta
I get File "D:/Desktop/Desktop/mygoogl", line 64, site = url.urlopen('google.com) File "C:\Python26\lib\urllib2.py", line 124, in urlopen return _opener.open(url, data, timeout)AttributeError: ProxyHandler instance has no attribute 'open'
Christopher
I missed a call to urllib2.build_opener()
dcrosta
+6  A: 
proxy= urllib2.ProxyHandler({'http': '127.0.0.1'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com')
ZelluX