urllib.urlopen isn't working. Is there a workaround?

python, url

Solution

You probably need to fill in proxy information.

import urllib2
proxy_handler = urllib2.ProxyHandler({'http': 'http://yourcorporateproxy:12345/'})
proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
opener.open('http://www.stackoverflow.com')

Problem

I'm getting a getaddress error and after doing some sleuthing, it looks like it might be my corporate intranet not allowing the connection (I'm assuming due to security, although it is strange that IE works but won't allow Python to open a url). Is there a safe way to get around this? Here's the exact error: ``` Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> b = urllib.urlopen('http://www.google.com') File "C:\Python26\lib\urllib.py", line 87, in urlopen return opener.open(url) File "C:\Python26\lib\urllib.py", line 203, in open return getattr(self, name)(url) File "C:\Python26\lib\urllib.py", line 342, in open_http h.endheaders() File "C:\Python26\lib\httplib.py", line 868, in endheaders self._send_output() File "C:\Python26\lib\httplib.py", line 740, in _send_output self.send(msg) File "C:\Python26\lib\httplib.py", line 699, in send self.connect() File "C:\Python26\lib\httplib.py", line 683, in connect self.timeout) File "C:\Python26\lib\socket.py", line 498, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): IOError: [Errno socket error] [Errno 11001] getaddrinfo failed ``` More info: I also get this error with urllib2.urlopen

Original source