from ftplib import FTP
def ftpconnect():
ftp_server = 'xxxxxxxxx'
username = 'xxxxxxxxxx'
password = 'xxxxxxxxxxxx'
ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect(ftp_server, 21)
ftp.login(username, password)
return ftp
def downloadfile(time = '20200521', filename = '20052108.000'):
remotepath = time + '\\GRAPES_3KM\\TMP\\2M_ABOVE_GROUND\\'
ftp = ftpconnect()
print(ftp.getwelcome())
localpath = 'D:\\launch1\\tem_forecast\\data\\Graps-3km\\'
file = open(localpath + filename, 'wb')
ftp.retrbinary('RETR' + remotepath + filename, file.write)
print(file.read())
file.close()
ftp.quit()
if __name__ == "__main__":
downloadfile(time='20200521', filename='20052108.000')
以下是输出以及报错信息
D:\program\anaconda\python.exe D:/launch1/tem_forecast/down_data.py
get ‘220 (vsFTPd 3.0.2)\n’
resp ‘220 (vsFTPd 3.0.2)’
cmd ‘USER MicapsData’
put ‘USER MicapsData\r\n’
get ‘331 Please specify the password.\n’
resp ‘331 Please specify the password.’
cmd ‘PASS ***************’
put ‘PASS ***************\r\n’
get ‘230 Login successful.\n’
resp ‘230 Login successful.’
welcome ‘220 (vsFTPd 3.0.2)’
220 (vsFTPd 3.0.2)
cmd ‘TYPE I’
put ‘TYPE I\r\n’
get ‘200 Switching to Binary mode.\n’
resp ‘200 Switching to Binary mode.’
cmd ‘PASV’
put ‘PASV\r\n’
get ‘227 Entering Passive Mode (xx,xxx,xx,xxx,xxx,xxx).\n’
resp ‘227 Entering Passive Mode (xx,xxx,xx,xxx,xxx,xxx).’
cmd ‘RETR20200521\GRAPES_3KM\TMP\2M_ABOVE_GROUND\20052108.000’
put ‘RETR20200521\GRAPES_3KM\TMP\2M_ABOVE_GROUND\20052108.000\r\n’
get ‘500 Unknown command.\n’
Traceback (most recent call last):
resp ‘500 Unknown command.’
File “D:/launch1/tem_forecast/down_data.py”, line 58, in
downloadfile(time=‘20200521’, filename=‘20052108.000’)
File “D:/launch1/tem_forecast/down_data.py”, line 26, in downloadfile
ftp.retrbinary(‘RETR’ + remotepath + filename, file.write)
File “D:\program\anaconda\lib\ftplib.py”, line 442, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File “D:\program\anaconda\lib\ftplib.py”, line 399, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File “D:\program\anaconda\lib\ftplib.py”, line 365, in ntransfercmd
resp = self.sendcmd(cmd)
File “D:\program\anaconda\lib\ftplib.py”, line 273, in sendcmd
return self.getresp()
File “D:\program\anaconda\lib\ftplib.py”, line 246, in getresp
raise error_perm(resp)
ftplib.error_perm: 500 Unknown command.
Process finished with exit code 1
我不知道500 Unknown command.是因为什么报错,查了很久,都找不到,各位藏龙卧虎,还请指点。