2

I have both Python 2.7.10 and Python 3.4.4 running on my computer. The following code works in Python 2.7.10:

import urllib2
print urllib2.urlopen('http://google.com').read()

However, if I move on to Python 3.4.4 and run

import urllib.request
print(urllib.request.urlopen('http://google.com').read())

I get a long pair of errors:

Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1183, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1137, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1182, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1133, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 963, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 898, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 871, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 516, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 507, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    print(urllib.request.urlopen('http://google.com').read())
  File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 464, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 482, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 442, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1211, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1185, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

I have come to understand that an error like this represents problems with the windows settings as suggested here (I'm running Windows 7), but what I can't understand is how it works in 2.7 and not in 3.4.

Community
  • 1
  • 1
Lucius Silanus
  • 139
  • 1
  • 11
  • 1
    Think they changed the package name... – linusg Jun 08 '16 at 18:32
  • 2
    Recommendation: just use [requests](http://docs.python-requests.org/en/master/). – Alyssa Haroldsen Jun 08 '16 at 18:33
  • With Python 3.2.3 on GNU/Linux this works fine. Perhaps Windows compilation issue... – kivy_student Jun 08 '16 at 18:41
  • I tested this locally (on Windows 7; Python 3.5.0) and it worked for me. – jacob Jun 08 '16 at 18:43
  • So this isn't a problem with the python code, but somewhere else. I'm tempted to simply use 2.7 for this project instead of trying to track down the issue, but I'd rather fix the problem than avoid it... – Lucius Silanus Jun 08 '16 at 18:45
  • I'd suggest using something like [Wireshark](https://www.wireshark.org/) to inspect the network traffic. – nephtes Jun 08 '16 at 18:48
  • @shivsn unlike the link you suggest, it is working in 2.7. This is actually the post that suggested to me it should be a windows setting issue instead of an issue with my code (I'll edit to reflect this). What I'm trying to figure out is how it works in 2.7 and not in 3.4... – Lucius Silanus Jun 08 '16 at 18:53
  • I tried it on ubuntu python 3.4.0 and it works. – shivsn Jun 08 '16 at 19:02

1 Answers1

0

After working at the solution, the Python 2.7 code began throwing the same error. Out of frustration I began applying the microscoft fix. I started by logging off and logging back in again. Everything works as intended now...

Lucius Silanus
  • 139
  • 1
  • 11