MCan
MCan

Reputation: 85

Connect to network not working using urllib or urllib2 even after configuring proxy

I am not able to open a url for read() using urllib or urllib2 even after using proxyhandlers (in case of urllib2) and setting proxies in urllib. My network which uses proxies to connect to internet have proxies (taken from my browser) is:

HTTP Proxy: someproxy.com Port: 1080

I have tried urllib:

import urllib
myproxies = {'http':'http://someproxy.com:1080'}
data = urllib.urlopen('http://www.google.com', proxies = myproxies).read()

but I am receiving this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\urllib.py", line 84, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 200, in open
    return self.open_unknown_proxy(proxy, fullurl, data)
  File "C:\Python27\lib\urllib.py", line 219, in open_unknown_proxy
  raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
IOError: [Errno socket error] [Errno 11001] getaddrinfo failed'

and for urllib2:

import urllib2
proxy = urllib2.ProxyHandler({'http':'http://someproxy.com:1080'})
opener1 = urllib2.build_opener(proxy)
urllib2.install_opener(opener1)
urllib2.urlopen('http://www.google.com')'

I am getting this error:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "C:\Python27\lib\urllib2.py", line 126, in urlopen
     return _opener.open(url, data, timeout)
   File "C:\Python27\lib\urllib2.py", line 394, in open
     response = self._open(req, data)
   File "C:\Python27\lib\urllib2.py", line 412, in _open
     '_open', req)
   File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
     result = func(*args)
   File "C:\Python27\lib\urllib2.py", line 1199, in http_open
     return self.do_open(httplib.HTTPConnection, req)
   File "C:\Python27\lib\urllib2.py", line 1174, in do_open
     raise URLError(err)
 urllib2.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

any help will be greatly appreciated.

MRick

Upvotes: 0

Views: 3324

Answers (1)

srgerg
srgerg

Reputation: 19339

I think you want the following for urllib

...
proxies = {'http':'http://someproxy.com:1080/'}
data = urllib.urlopen('http://www.google.com', proxies=proxies).read()
...

or this for urllib2:

...
proxy = urllib2.ProxyHandler({'http':'http://someproxy.com:1080'})
...

Note the proxy url includes the protocol part, which your code omits.

Upvotes: 2

Related Questions