复制代码 代码如下:
import os
import sys
import ftplib
import socket
##################################################################
#################################################################
def getServerLog(dir,fileName,host,userName,password):
if os.path.exists(fileName):
print '****the file '+ fileName +' has already exist! The file will be over writed'
try:
f=ftplib.FTP(host)
except (socket.error,socket.gaierror),e:
print '----ERROR:cannot reach '+host
print e
return False
try:
f.login(user=userName,passwd=password)
except ftplib.error_perm ,e:
print '----ERROR:cannot login to server '+host
print e
f.quit()
return False
print '****Logged in as ' + userName + ' to server ' +host
try:
f.cwd(dir)
except ftplib.error_perm,e:
print '----ERROR:cannot CD to %s on %s' % (dir,host)
print e
f.quit()
return False
print '**** changed to %s folder on %s' % (dir,host)
try:
f.retrbinary('RETR %s' % fileName,open(fileName,'wb').write)
except ftplib.error_perm,e:
print '----ERROR:cannot read file %s on %s' % (fileName,host)
print e
os.unlink(fileName)
return False
else:
print '****Downloaded '+ fileName +' from '+ host +' to '+os.getcwd()
f.quit()
return True
if name == "main":
getServerLog("/userhome/root/other/temp","a.out","10.10.10.10","root","password")
print '****done'
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8