ftp 下载文件异常,只可以下载文件名字
def download_file(self, localfile, remotefile):
try:
ftp=FTP()
file_handler = open(localfile, 'wb')
ftp.retrbinary(u'RETR %s'%(remotefile), file_handler.write)
file_handler.close()
except Exception:
print u'down load failed'
def my_ftp(self)
hostaddr=192.168.0.166
username=yyyy
password=yyyy
port=21
try:
ftp=FTP()
timeout = 300
print timeout
socket.setdefaulttimeout(timeout)
print timeout
ftp.set_pasv(True)
print u'start connect %s' %(hostaddr)
print hostaddr
ftp.connect(hostaddr,port)
print hostaddr
print u'sucess connect %s' %(hostaddr)
ftp.login(username,password)
print u'login in %s' %(hostaddr)
print u'connect or login failed'
except Exception:
print u'connect or login failed'
try:
ftp.cwd(/)
print remotedir
except Exception:
print u'change directory failed'
if not os.path.isdir(/data):
os.makedirs(/data)
self.file_list = []
ftp.dir(self.get_file_list)
remotenames = self.file_list
for item in remotenames:
filetype=item[0]
filename=item[1]
local=os.path.join(localdir, filename)
print local
if filetype == 'd':
return
elif filetype == '-':
self.download_file(local, filename)
def get_file_list(self, line):
ret_arr = []
file_arr = self.get_filename(line)
if file_arr[1] not in ['.', '..']:
self.file_list.append(file_arr)
def get_filename(self, line):
pos = line.rfind(':')
while(line[pos] != ' '):
pos += 1
while(line[pos] == ' '):
pos += 1
file_arr = [line[0],line[pos:]]
return file_arr
异常出现在 def download_file 中。
[解决办法]
没细看楼主的代码,share我的类库一同等板砖
#-*- coding: utf-8 -*-from ftplib import FTPimport os.pathimport reclass FtpConnect: def __init__(self, **kwg): self.setting = dict(kwg) def __enter__(self): self.host = FTP(host = self.setting['host']) self.host.login(user = self.setting['user'], passwd=self.setting['password']) if self.setting.get('dataroot'): self.host.cwd(self.setting.get('dataroot')) self.dirList() return self def __exit__(self, *args): self.host.close() def cwd(self, local_dir='.', remote_dir='.'): self.dir = {'Local': local_dir, 'Remote': remote_dir,} self.host.cwd(remote_dir) self.dirList() def dirList(self): lstpatt = re.compile(r'(.{18})\s+(\S+)\s+(.*)') data, self.subdirs, self.files = [], [], [] self.host.dir(data.append) for line in data: match = lstpatt.search(line) (dt, tp_sz, name) = match.groups() if tp_sz == '<DIR>': self.subdirs.append(name) else: self.files.append(name) def get(self, fname): localFile = open(os.path.join(self.dir['Local'], fname), 'wb') command = "RETR " + fname try: self.host.retrbinary(command, localFile.write) except: pass localFile.close() def put(self, fname): localFile = open(os.path.join(self.dir['Local'], filename), 'rb') command = "STOR " + fname try: self.host.storbinary(command, localFile) except: pass localFile.close() def getAs(self, fname, localfname): localFile = open(os.path.join(self.dir['Local'], localfname), 'wb') command = "RETR " + fname try: self.host.retrbinary(command, localFile.write) except: pass localFile.close()
[解决办法]
def download_file(self, ftp, localfile, remotefile): try: file_handler = open(localfile, 'wb') ftp.retrbinary(u'RETR %s'%(remotefile), file_handler.write) file_handler.close() except Exception: print u'down load failed'