[Tutor] fortify this code...
Tim Wilson
wilson@chemsun.chem.umn.edu
Thu, 21 Oct 1999 18:13:12 -0500 (CDT)
Hi everyone,
I'm working on a little portal-like feature for my Zope site. I'm going to
download the local weather conditions from the nearest National Weather
Service station and make them available for display on our Web page. This
is based on the pyWeather program. Here's the code:
--snip--
#!/usr/local/bin/python
##################################################################
# This program gets the current weather conditions from NOAA.
#
# Change the 'metar_id' variable to match your nearest METAR station.
# Check http://www.nws.noaa.gov/oso/siteloc.shtml for a listing
#
# This code for this program was derived in part from pyWeather by
# "The Almighty Groundhog" (groundhog@linuxfreak.com)
#
# Current version by Tim Wilson (wilson@chem.umn.edu)
##################################################################
from ftplib import *
import string, os, getpass
from sys import *
# Do some variable inits
noaa_url = "weather.noaa.gov"
metar_dir = "/data/observations/metar/decoded/"
station_id = 'KSGS' # South St. Paul, MN, USA
tmp = '.metar_data'
# Get .pyweatherdat file ready
if os.path.exists(tmp) == 1:
os.remove(tmp)
data = open(tmp, 'w')
# Do the FTP get
ftp = FTP(noaa_url)
ftp.login()
ftp.cwd(metar_dir)
ftp.retrbinary('RETR ' + station_id + '.TXT', data.write)
ftp.quit
data.close()
--snip--
This code works well (assuming your interested in the local weather in St.
Paul, MN :-).
It crashes and burns, however, if the NOAA ftp server is busy or otherwise
unavailable. How can I make this script retry the connection a few times
and failing that, exit gracefully. I also notice that I'll have to change
it so that the old file isn't overwritten until the new data is available.
Thanks for any advice that any of you might have.
-Tim
--
Timothy Wilson | "The faster you | Check out:
Henry Sibley H.S. | go, the shorter | http://slashdot.org/
W. St. Paul, MN, USA | you are." | http://linux.com/
wilson@chem.umn.edu | -Einstein | http://www.mn-linux.org/