37,742
社区成员
发帖
与我相关
我的任务
分享
:: python script to download selected files from rda.ucar.edu
::
import sys
import os
import urllib2
import cookielib
::
if (len(sys.argv) != 2):
print "usage: "+sys.argv[0]+" [-q] password_on_RDA_webserver"
print "-q suppresses the progress message for each file that is downloaded"
sys.exit(1)
::
passwd_idx=1
verbose=True
if (len(sys.argv) == 3 and sys.argv[1] == "-q"):
passwd_idx=2
verbose=False
::
cj=cookielib.MozillaCookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
::
:: check for existing cookies file and authenticate if necessary
do_authentication=False
if (os.path.isfile("auth.rda.ucar.edu")):
cj.load("auth.rda.ucar.edu",False,True)
for cookie in cj:
if (cookie.name == "sess" and cookie.is_expired()):
do_authentication=True
else:
do_authentication=True
if (do_authentication):
login=opener.open("https://rda.ucar.edu/cgi-bin/login","email=scienceliaocheng@sina.com&password="+sys.argv[1]+"&action=login")
::
:: save the authentication cookies for future downloads
:: NOTE! - cookies are saved for future sessions because overly-frequent authentication to our server can cause your data access to be blocked
cj.clear_session_cookies()
cj.save("auth.rda.ucar.edu",True,True)
::
:: download the data file(s)
listoffiles=["grib2/2017/2017.06/fnl_20170602_00_00.grib2","grib2/2017/2017.06/fnl_20170602_06_00.grib2","grib2/2017/2017.06/fnl_20170602_12_00.grib2"]
for file in listoffiles:
idx=file.rfind("/")
if (idx > 0):
ofile=file[idx+1:]
else:
ofile=file
if (verbose):
sys.stdout.write("downloading "+ofile+"...")
sys.stdout.flush()
infile=opener.open("http://rda.ucar.edu/data/ds083.2/"+file)
outfile=open(ofile,"wb")
outfile.write(infile.read())
outfile.close()
if (verbose):
sys.stdout.write("done.\n")