Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 - urllib, HTTP Error 407: Proxy Authentication Required

I'm trying to open a website (I am behind a corporate proxy) using urllib.request.urlopen() but I am getting the error:

urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required

I can find the proxy in urllib.request.getproxies(), but how do I specify a username and password to use for it? I couldn't find the solution in the official docs.

like image 325
Lanaru Avatar asked Aug 01 '12 15:08

Lanaru


1 Answers

import urllib.request as req

proxy = req.ProxyHandler({'http': r'http://username:password@url:port'})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
conn = req.urlopen('http://google.com')
return_str = conn.read()
like image 196
Lanaru Avatar answered Oct 21 '22 14:10

Lanaru