0

I'm trying to use python's urllib class to programmatically access site resources. No matter what site I use, however, I receive a timeout error.

What's wrong with this code?

url = "http://en.wikipedia.org/wiki/List_of_HTTP_header_fields"
fileLoc = "directory\\" + url.rpartition("/")[2]
request = Request(url)
request.add_header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)")
response = urlopen(request)
data = response.read()
text = data.decode('utf-8')
file = open(fileLoc, "w+")
file.write(text)
file.close

The error occurs in the urlopen(request) method. Any help is greatly appreciated.

cscan
  • 3,684
  • 9
  • 45
  • 83
  • 1
    Are you behind a firewall or forced proxy? – dhke Mar 20 '15 at 17:11
  • Not behind a proxy. I am behind the windows firewall. That shouldn't affect standard http traffic, though. – cscan Mar 20 '15 at 17:17
  • You could open a command prompt and try to use `telnet en.wikipedia.org 80` (you might need to install it via windows features). If that command also times out, the problem might actually be at the network layer. – dhke Mar 20 '15 at 17:19
  • telnet fails, it looks to be network related. Is there anything that I can do to get past this problem? – cscan Mar 20 '15 at 17:56
  • Strange, because the fact that you are posting this means you have browser based internet connectivity. Check your browser's proxy settings. You can find information on setting the proxy on python here: https://stackoverflow.com/questions/17247981/how-to-tell-python-to-automatically-use-the-proxy-setting-in-windows-xp-like-rs. Other than that I'm at a loss. – dhke Mar 20 '15 at 18:11

1 Answers1

0

I was in fact behind a forced proxy, as suggested by @dhke.

cscan
  • 3,684
  • 9
  • 45
  • 83