Frage

I'm trying to follow the suggestions from this thread, but its not working. At this point, I just wanted to perform a very simple test to make sure that I was actually getting the data returned from the site I'm trying to open. For my simple test I was trying to open the Yahoo weather api. I've verified that typing in this address in a web browser does indeed return data. I've tried both of these code snippets and neither one is working.

import urllib

params = urllib.urlencode({'w': 2482950})
f = urllib.urlopen("http://weather.yahooapis.com/forecastrss?%s" % params)
print f.read()

This example came straight from the Python website. This dies with:

IOError: [Errno socket error] [Errno 110] Connection timed out

I also tried using httplib like this:

import httplib

conn = httplib.HTTPConnection(host='weather.yahooapis.com', timeout=10)
req = '/forecastrss?w=2482950'
try:
    conn.request('GET',req)
except:
    print "Didn't work"

content = conn.getresponse().read()
print content

Trying this gives me the following error:

  self.fp = sock.makefile('rb', 0)
AttributeError: 'NoneType' object has no attribute 'makefile'

It appears that I'm not ever making the connection to the remote host. Any ideas what I'm doing wrong?

War es hilfreich?

Lösung

The problem was indeed the firewall. I added the following code to get it to work:

proxy_support = urllib2.ProxyHandler({"http":"http://<proxy>:<port>"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

html = urllib2.urlopen(url).read()
print html
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top