Note that there are some explanatory texts on larger screens.

plurals
  1. POUrllib and urllib2 returns "IOError: [Errno socket error] [Errno -2] Name or service not known" but Firefox downloads file with no trouble
    text
    copied!<p>I am trying to download GRIB data (binary weather forecast data) from the National Weather Service. I have written Python code to format the HTTP string to get data for today, looking 12 hours ahead.</p> <p>The Python code returns the HTTP string, then attempts to use urllib.urlopen to download the data. Now, if I paste the HTTP string into Firefox, the GRIB file downloads. If I try to use urllib.urlopen, I get the following:</p> <pre><code>Traceback (most recent call last): File "/home/dantayaga/bovine_aerospace/dev/grib_get.py", line 67, in &lt;module&gt; webf=urllib.urlopen(griburl) File "/usr/lib/python2.7/urllib.py", line 86, in urlopen return opener.open(url) File "/usr/lib/python2.7/urllib.py", line 207, in open return getattr(self, name)(url) File "/usr/lib/python2.7/urllib.py", line 344, in open_http h.endheaders(data) File "/usr/lib/python2.7/httplib.py", line 954, in endheaders self._send_output(message_body) File "/usr/lib/python2.7/httplib.py", line 814, in _send_output self.send(msg) File "/usr/lib/python2.7/httplib.py", line 776, in send self.connect() File "/usr/lib/python2.7/httplib.py", line 757, in connect self.timeout, self.source_address) File "/usr/lib/python2.7/socket.py", line 553, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): IOError: [Errno socket error] [Errno -2] Name or service not known </code></pre> <p>Here is the HTTP string I am using:</p> <p><a href="http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_hd.pl?file=gfs.t06z.mastergrb2f12&amp;lev_1000_mb=on&amp;lev_975_mb=on&amp;lev_950_mb=on&amp;lev_925_mb=on&amp;lev_900_mb=on&amp;lev_850_mb=on&amp;lev_800_mb=on&amp;lev_750_mb=on&amp;lev_700_mb=on&amp;lev_650_mb=on&amp;lev_600_mb=on&amp;lev_550_mb=on&amp;lev_500_mb=on&amp;lev_450_mb=on&amp;lev_400_mb=on&amp;lev_350_mb=on&amp;lev_300_mb=on&amp;lev_250_mb=on&amp;lev_200_mb=on&amp;lev_150_mb=on&amp;lev_100_mb=on&amp;lev_70_mb=on&amp;lev_30_mb=on&amp;lev_20_mb=on&amp;lev_10_mb=on&amp;var_HGT=on&amp;var_RH=on&amp;var_TMP=on&amp;var_UGRD=on&amp;var_VGRD=on&amp;var_VVEL=onleftlon=-90rightlon=90toplat=90bottomlat-90&amp;dir=%2Fgfs.2012070706%2Fmaster" rel="nofollow">http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_hd.pl?file=gfs.t06z.mastergrb2f12&amp;lev_1000_mb=on&amp;lev_975_mb=on&amp;lev_950_mb=on&amp;lev_925_mb=on&amp;lev_900_mb=on&amp;lev_850_mb=on&amp;lev_800_mb=on&amp;lev_750_mb=on&amp;lev_700_mb=on&amp;lev_650_mb=on&amp;lev_600_mb=on&amp;lev_550_mb=on&amp;lev_500_mb=on&amp;lev_450_mb=on&amp;lev_400_mb=on&amp;lev_350_mb=on&amp;lev_300_mb=on&amp;lev_250_mb=on&amp;lev_200_mb=on&amp;lev_150_mb=on&amp;lev_100_mb=on&amp;lev_70_mb=on&amp;lev_30_mb=on&amp;lev_20_mb=on&amp;lev_10_mb=on&amp;var_HGT=on&amp;var_RH=on&amp;var_TMP=on&amp;var_UGRD=on&amp;var_VGRD=on&amp;var_VVEL=onleftlon=-90rightlon=90toplat=90bottomlat-90&amp;dir=%2Fgfs.2012070706%2Fmaster</a></p> <p>If you are testing this string in Firefox and it's not working, change "20120707" to today's date and "06" to "00" and it should work.</p> <p>My question is simple (I think): why does this work in Firefox and not with urllib? </p> <p>Here is the code I use to generate the http string and then attempt to download the result:</p> <pre><code>#Get GRIB files import urllib forecast_time='06' #What time the forecast is (00, 06, 12, 18) forecast_hours='12' #How many hours ahead to forecast (2 or 3 digits) forecast_date='20120707' #What date the forecast is for yyyymmdd top_lat=90 #Top of bounding box (North) bottom_lat=-90 #Bottom of bounding box (South) left_lon=-90 #Left of bounding box (West) right_lon=90 #Right of bounding box (East) griburl='http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_hd.pl?' griburl=griburl+'file=gfs.t'+str(forecast_time)+'z.mastergrb2f' griburl=griburl+forecast_hours #Select atmospheric levels griburl=griburl+'&amp;lev_1000_mb=on' #1000 mb level griburl=griburl+'&amp;lev_975_mb=on' #975 mb level griburl=griburl+'&amp;lev_950_mb=on' #950 mb level griburl=griburl+'&amp;lev_925_mb=on' #925 mb level griburl=griburl+'&amp;lev_900_mb=on' #900 mb level griburl=griburl+'&amp;lev_850_mb=on' #850 mb level griburl=griburl+'&amp;lev_800_mb=on' #800 mb level griburl=griburl+'&amp;lev_750_mb=on' #750 mb level griburl=griburl+'&amp;lev_700_mb=on' #700 mb level griburl=griburl+'&amp;lev_650_mb=on' #650 mb level griburl=griburl+'&amp;lev_600_mb=on' #600 mb level griburl=griburl+'&amp;lev_550_mb=on' #550 mb level griburl=griburl+'&amp;lev_500_mb=on' #500 mb level griburl=griburl+'&amp;lev_450_mb=on' #450 mb level griburl=griburl+'&amp;lev_400_mb=on' #400 mb level griburl=griburl+'&amp;lev_350_mb=on' #350 mb level griburl=griburl+'&amp;lev_300_mb=on' #300 mb level griburl=griburl+'&amp;lev_250_mb=on' #250 mb level griburl=griburl+'&amp;lev_200_mb=on' #200 mb level griburl=griburl+'&amp;lev_150_mb=on' #150 mb level griburl=griburl+'&amp;lev_100_mb=on' #100 mb level griburl=griburl+'&amp;lev_70_mb=on' #70 mb level griburl=griburl+'&amp;lev_30_mb=on' #30 mb level griburl=griburl+'&amp;lev_20_mb=on' #20 mb level griburl=griburl+'&amp;lev_10_mb=on' #10 mb level #Select variables griburl=griburl+'&amp;var_HGT=on' #Height (geopotential m) griburl=griburl+'&amp;var_RH=on' #Relative humidity (%) griburl=griburl+'&amp;var_TMP=on' #Temperature (K) griburl=griburl+'&amp;var_UGRD=on' #East-West component of wind (m/s) griburl=griburl+'&amp;var_VGRD=on' #North-South component of wind (m/s) griburl=griburl+'&amp;var_VVEL=on' #Vertical Windspeed (Pa/s) #Select bounding box griburl=griburl+'leftlon='+str(left_lon) griburl=griburl+'rightlon='+str(right_lon) griburl=griburl+'toplat='+str(top_lat) griburl=griburl+'bottomlat'+str(bottom_lat) #Select date and time griburl=griburl+'&amp;dir=%2Fgfs.'+forecast_date+forecast_time+'%2Fmaster' print(griburl) print('Downloading GRIB file for date '+forecast_date+' time ' +forecast_time + ', forecasting '+forecast_hours+' hours ahead...') webf=urllib.urlopen(griburl) local_filename=forecast_date+'_'+forecast_time+'_'+forecast_hours+'.grib' localf=open('//home//dantayaga//bovine_aerospace//grib//data//'+local_filename, 'wb') localf.write(webf.read()) print('Requested grib data written to file '+local_filename) </code></pre> <p>Any help is most appreciated. Is there a formatting error that Firefox is catching or something?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload