版权声明:自由转载,无需过问 https://blog.csdn.net/Next__One/article/details/78644094
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from ftplib import FTP
# ftp 服务器链接
def ftpconnect():
ftp_server = 'ftp.ncdc.noaa.gov'
username = ''
password = ''
ftp = FTP()
ftp.set_debuglevel(2) # 打开调试级别2,显示详细信息
ftp.connect(ftp_server, 21) # 连接
ftp.login(username, password) # 登录,如果匿名登录则用空串代替即可
return ftp
# 开始下载文件
def downloadfile(start, end,srcpath):
ftp = ftpconnect()
# print ftp.getwelcome() #显示ftp服务器欢迎信息
datapath = "/pub/data/gsod/"
while start <= end:
path = datapath + str(start)
li = ftp.nlst(path)
# 创建指定年份的目录
path = srcpath + '/'
dir = str(start)
new_path = os.path.join(path, dir)
if not os.path.isdir(new_path):
os.makedirs(new_path)
for eachFile in li:
localpaths = eachFile.split("/")
localpath = localpaths[len(localpaths) - 1]
localpath = new_path + '/' + str(start) + '--' + localpath # 把日期放在最前面,方便排序
bufsize = 1024 # 设置缓冲块大小
fp = open(localpath, 'wb') # 以写模式在本地打开文件
ftp.retrbinary('RETR ' + eachFile, fp.write, bufsize) # 接收服务器上文件并写入本地文件
start = start + 1
ftp.set_debuglevel(0) # 关闭调试
ftp.close()
ftp.quit() # 退出ftp服务器
if __name__ == "__main__":
downloadfile(1950,2017,"g:/c/ncdc")