Python ftplib,通过FTP获取最新文件
我知道怎么设置下载一个文件,比如说文件名是file-LATEST,但大多数文件的名字并不是这样。我该怎么才能下载到最新的文件呢?
可以通过以下几种方式来实现:
- 查看修改日期
- 查看时间戳
如果当前版本小于新版本,就下载
这是我目前的进展:
#!/usr/bin/env python
import ftplib
import os
import socket
HOST = 'ftp.site.org'
DIRN = 'dir/'
FILE = 'filename-LATEST.tar.gz'
def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
print 'ERROR: cannot reach "%s"' % HOST
return
print '*** Connected to host "%s"' % HOST
try:
f.login()
except ftplib.error_perm:
print 'ERROR:: cannot login anonymously'
f.quit()
return
print '*** Logged in as "anonymous"'
try:
f.cwd(DIRN)
except ftplib.error_perm:
print 'ERROR: cannot CD to "%s"' % DIRN
f.quit()
return
print '*** Changed to "%s" folder' % DIRN
try:
f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
except ftplib.error_perm:
print 'ERROR: cannot read file "%s"' % FILE
os.unlink(FILE)
else:
print '*** Downloaded "%s" to CWD' % FILE
f.quit()
return
if __name__ == '__main__':
main()
2 个回答
-1
我的回答和 ftplib
没关系。这是用 pexpect
的方法。下面是代码
#!/usr/bin/python -B
#Program to download latest file from a FTP directory specified
import sys
import os
import pexpect
if len(sys.argv)!=5:
print "Usage:"
print "\t",sys.argv[0],"<IP Address> <Username> <Password> <Directory-Path>"
print "\t","Use \"\" if there is no password"
sys.exit(1)
ip=str(sys.argv[1])
usr=str(sys.argv[2])
pswd=str(sys.argv[3])
path=str(sys.argv[4])
timeout=10
log_file=file("temp",'w')
try:
try:
child=pexpect.spawn("ftp "+ip)
except:
print "failed to connect "+sys.exc_function()
sys.exit(1)
x=child.expect(['Name',pexpect.EOF,pexpect.TIMEOUT],timeout)
if x!=0:
print "No \"Name\" prompt"
sys.exit(1)
child.sendline(usr)
x=child.expect(["[pP]assword","[lL]ogin [sS]ucess","logged in",
pexpect.EOF,pexpect.TIMEOUT],timeout)
if x==0:
child.sendline(pswd) #Add case for no password
y=child.expect(["[lL]ogin [sS]uccess","User "+usr+" logged in",
"[iI]correct [lLogin]",pexpect.EOF,pexpect.TIMEOUT],timeout)
if y!=0 and y!=1:
print "Login Failed"
sys.exit(1)
elif x!=1 and x!=2:
print "Authentication Timed Out!!"
sys.exit(1)
child.sendline("cd "+path)
x=child.expect(["[dD]irectory successfully changed","CWD command successful",
pexpect.EOF,pexpect.TIMEOUT],timeout)
if x!=0 and x!=1:
print "cd Failed!!!"
sys.exit(1)
child.logfile=log_file
child.sendline("ls -lrt")
x=child.expect(["[dD]irectory send OK","[tT]ransfer [cC]omplete",
pexpect.EOF,pexpect.TIMEOUT],timeout)
if x!=0 and x!=1:
print "listing failed"
sys.exit(1)
os.system("cat temp | grep '^-' |"+\
"sed -n '$p' | awk '{print $NF}' > file_name")
fd=open("file_name","r") #for loop is not needed here since there'll be only one line
for line in fd:
if line:
latest_file=line.strip()
break
else:
print "No file found"
sys.exit(1)
fd.close()
child.logfile=sys.stdout
os.system("rm temp file_name")
child.sendline("bi")
child.expect("ftp>")
child.sendline("ha")
child.expect("ftp>")
child.sendline("get "+latest_file)
x=child.expect(["[tT]ransfer [cC]omplete",
pexpect.EOF,pexpect.TIMEOUT],200) #Add more error cases here
if x!=0:
print "Copy Failed"
sys.exit(1)
child.close()
sys.exit(0)
except KeyboardInterrupt:
print "",
ftp
的提示信息在不同的 ftp 服务器上会有所不同。你可以先手动试一次,然后在代码中用 expect
来匹配同样的提示。你也可以添加更多的错误处理情况。我使用 log_file
是因为我不能直接在 ftp 中通过管道输出 ls -lrt
来提取文件名。
我知道我的代码效率不高,欢迎随意修改。不过这个方法是可以用的!!加油!
0
你需要像ftplib文档里说的那样,使用 ftp.retrlines('LIST',callback)
这个命令,然后解析返回的结果。