0
votes

I'm trying to run a simple Python script which connects to a website and reads a document on the website, but it fails. Any help, would be most welcome, thank you!

The code is as follows:

import urllib.request

fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')

for line in fhand:
    print (line.strip())

And I'm getting the following two error messages:

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

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}

2
works perfectly fine for me, must be a problem with your your connection settings. Do you have a proxy? - JDurstberger

2 Answers

1
votes

This is the windows error, not urllib's one. The latter has a handy default timeout value of never, but the former depends on settings of your proxy (with default being 60 seconds for initial connection and 120 for any further GET requests.

I can't help much with windows, but at least now you know where to look.

0
votes

I had this same issue, and tried specifying a timeout in the urllib.request.urlopen, but no dice.

What did work was including a header, as described here. The first answer worked, and so did the second, so I used the simpler, second solution.