Python: urllib2 error "Name or server not known"

3k views Asked by At

I have a script that checks the local weather and outputs a string with the conditions.

The script works fine on my Mac but when I run it on my Raspberry Pi over SSH it returns this error:

urllib2.URLError: <urlopen error [Errno -2] Name or service not known>

Here is the part of the script returning the error:

import urllib2
import json
import fnmatch
key='xxxxxxxxxxxxxxxx'
url='http://api.wunderground.com/api/%s/geolookup/conditions/q/PA/%s.json' %(key, zipCode)
f=urllib2.urlopen(url)
json_string = f.read()
parsed_json=json.loads(json_string)
city=parsed_json['location']['city']
state=parsed_json['location']['state']
weather=parsed_json['current_observation']['weather']
temperature = parsed_json['current_observation']['temperature_string']
report=("The current weather in " + str(city) + " " + str(state) + " is " + str(weather.lower()) + " and " + str(temperature)).replace("F (","degrees fahrenheit or ").replace("C)","degrees celsius")
return report

and here is the full error:

File "/home/pi/Desktop/Programming/Python/WeatherAlarm.py", line 77, in <module>
    opening()
  File "/home/pi/Desktop/Programming/Python/WeatherAlarm.py", line 52, in opening
    wait(alarmtime, platform, sound, zipCode)       
  File "/home/pi/Desktop/Programming/Python/WeatherAlarm.py", line 75, in wait
    alarm(platform, sound, zipCode)
  File "/home/pi/Desktop/Programming/Python/WeatherAlarm.py", line 58, in alarm
    say=Wunder(zipCode)
  File "/home/pi/Desktop/Programming/Python/WeatherAlarm.py", line 12, in Wunder
    f=urllib2.urlopen(url)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 401, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 419, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1211, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1181, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>

I am using Wunderground's API for my weather report.

UPDATE

I have found, thanks to some commenters, that the problem is that when I enter:

curl api.wunderground.com

in the RPi's command line, it returns can't resolve hostname. Though it works fine on my Mac. What is the problem?

Also, this was working a day ago on my Pi as well as my Mac.

2

There are 2 answers

0
junnytony On BEST ANSWER

The issue is that your rasberry-pi can't resolve the domain name http://api.wunderground.com. There are possibly two reasons for this.

  1. It's not connected to the internet (direct wifi/ethernet or shared connection)
  2. It's DNS servers are not configured so it doesn't know who to contact to resolve the domain name into an IP address

If you're sure your RPi is connected to the internet, I would test theory 2 by pinging google.com and then pinging 8.8.8.8 (public DNS server) from your RPi.

0
Eric On

Sometimes, you are using a proxy without knowing it :

You should check proxy env variables :

$ env | grep -i proxy
http_proxy=http://someip:1757     
https_proxy=https://someip:1757

If you do not have a proxy, Check your .bashrc and remove their definition. (my case : it was a bad cut&paste from another desktop .bashrc).