Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python urllib.request.urlopen() returning error 10061?

Tags:

python

urllib

I'm trying to download the HTML of a page (http://www.google.com in this case) but I'm getting back an error. Here is my interactive prompt session:

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib.request
>>> html = urllib.request.urlopen("http://www.google.com")
Traceback (most recent call last):
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1136, in
 do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 964, in req
uest
    self._send_request(method, url, body, headers)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 1002, in _s
end_request
    self.endheaders(body)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 960, in end
headers
    self._send_output(message_body)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 805, in _se
nd_output
    self.send(msg)
  File "\\****.****.org\myhome\python\lib\http\client.py", line 743, in sen
d
    self.connect()
  File "\\****.****.org\myhome\python\lib\http\client.py", line 721, in con
nect
    self.timeout, self.source_address)
  File "\\****.****.org\myhome\python\lib\socket.py", line 404, in create_c
onnection
    raise err
  File "\\****.****.org\myhome\python\lib\socket.py", line 395, in create_c
onnection
    sock.connect(sa)
socket.error: [Errno 10061] No connection could be made because the target machi
ne actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 138, in
urlopen
    return opener.open(url, data, timeout)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 369, in
open
    response = self._open(req, data)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 387, in
_open
    '_open', req)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 347, in
_call_chain
    result = func(*args)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1156, in
 http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1139, in
 do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 10061] No connection could be made
because the target machine actively refused it>
>>>

My best guess is that my network's firewall is blocking the connection (most/all ports other than 80 are blocked). I don't see why, though; doesn't Python connect on port 80? Does anyone know what is happening?

like image 219
HellaMad Avatar asked Mar 06 '12 15:03

HellaMad


2 Answers

urllib takes the proxy settings from Internet Explorer, which is usually under Tools->Internet Options->Connections->Lan Settings

If you have a proxy, make sure it's correct in IE, or set it when using urllib. If you don't use a proxy to browse, make sure that the proxy settings in IE are empty.

I was struggling with this issue few hours until I realized that a 3rd party app I once installed changed the setting in IE, while i was surfing happily in Firefox without a proxy.


Another solution is in the this link which shows how to force urllib to ignore IE's proxy settings

like image 178
michaelgo Avatar answered Sep 30 '22 17:09

michaelgo


The error comes from operating system level and is not related to Python. It would be same with any programming language.

Contact your network administration to see what's the matter unless you can resolve firewall issues yourself. If you need a HTTP proxy then you can configure one for urllib.

like image 23
Mikko Ohtamaa Avatar answered Sep 30 '22 19:09

Mikko Ohtamaa