vsftpd启动问题简记
centos7 能以ipv6方式启动,启动只需修改配置如下
如需同时启动到ipv4跟ipv6,需拷贝配置文件,一份配置中只监听ipv4,一份配置中只监听ipv6
centos6中无法启动到ipv6,错误如下,20,21端口也没被其它程序占用:
以ipv4的方式启动vsftpd,在获取文件信息,下载文件,只能以主动模式运行,而ipv6可以以被动模式运行
python中ftplib进行ftp的代码(下载文件,获取文件信息),设为主动模式i,在window能正常运行,但是放到Linux下就跑不起来,会报“500 Illegal PORT command”,如果使用ftp工具,目标服务器是能进行文件上传下载的,具体需要看下ftplib的说明文档
写了一个工具类,有点乱
#encoding=utf8 import os, re, ftplib from config import MRedis, MCoding class MFTP(): def __init__(self, host, username=None, password=None, del_remote=False, pasv=False): self.host = host self.username = username self.password = password self.del_remote = del_remote self.ftp = ftplib.FTP(host) self.ftp.set_pasv(pasv) self.ftp.login() if self.username==None else self.ftp.login(username, password) def download_file(self, remote_url, local_dir):#centos try: file_name = remote_url.split('/').pop() local_file = (local_dir + '/' + remote_url).decode('utf8', "ignore").encode(MCoding, "ignore") remote_url = remote_url.replace(file_name, '') self.ftp.cwd('~/' + remote_url) buffsize = 1024 fp = open(local_file, 'wb') self.ftp.retrbinary('RETR %s' % file_name, fp.write, buffsize) fp.close() if self.del_remote: print '删除文件:', remote_url, ':', file_name, '当前路径:', self.ftp.pwd() self.ftp.delete(file_name) except BaseException as e: print '发生异常:', e def download_dir(self, remote_url, local_dir):#centos try: local_url = local_dir + '/' + remote_url local_url = local_url.decode('utf8', "ignore").encode(MCoding, "ignore") print '远程路径', remote_url print '本地路径', local_url self.ftp.cwd('~/' + remote_url) if not os.path.exists(local_url): os.mkdir(local_url) dirs = [] self.ftp.dir('', '.', dirs.append) for i in dirs: f_name = i.split(' ').pop() rusult = re.match('^dr.*', i) if rusult: print '文件夹下载:', remote_url self.download_dir(remote_url + '/' + f_name, local_dir) else: print '文件下载:', remote_url, '/', i self.download_file(remote_url + '/' + f_name, local_dir) if self.del_remote: print '删除文件夹:', remote_url self.ftp.rmd(remote_url.split('/')[-1]) except BaseException as e: print '下载文件夹出错', e def getDirInfo(self, path):#获取目录下所有文件跟文件夹 self.ftp.cwd(path) result = {'files': [], 'dirs': []} dirs = [] self.ftp.dir('', '.', dirs.append) for dir in dirs: name = dir.split(' ').pop() if re.match('^dr.*', dir): result['dirs'].append(name) else: result['files'].append(name) return result def upload_file(self, file_name, remote_path, local_path): bufsize = 1024 fp = open(local_path+'/'+file_name, 'rb') self.ftp.storbinary('STOR ' + remote_path+'/'+file_name, fp, bufsize) self.ftp.set_debuglevel(0) def close(self): self.ftp.close() def down_dir_test(local_url):#下载测试 host = '***' username = '***' password = '***' mftp = MFTP(host, username, password, True) mftp.download_dir('', local_url) def del_dir(ftp, dirName):#删除文件夹 ftp.cwd(dirName) def down_all(local_url):#同步文件到本地 from config import dbsession slaves = dbsession.execute('select ftp_host, ftp_user, ftp_pwd, centos from slave') for slave in slaves: print slave.ftp_host, slave.ftp_user, slave.ftp_pwd MRedis.hset('ftp', slave.ftp_host, 'ftping') mftp = MFTP(slave.ftp_host, slave.ftp_user, slave.ftp_pwd, pasv='centos7' in slave.centos) try: print '本地路径', local_url mftp.download_dir('file', local_url) except BaseException as e: print 'error', e finally: MRedis.hset('ftp', slave.ftp_host, 'complete') mftp.close() def upload_file(file_name, remote_path, local_path):#上传文件测试 host = '***' # host = '***' username = '***' password = '***' mftp = MFTP(host, username, password, True) mftp.upload_file(file_name, remote_path, local_path) mftp.close() def upload_file_all(file_name, remote_path, local_path):#同步爬虫文件到其他从机 from config import dbsession slaves = dbsession.execute('select ftp_host, ftp_user, ftp_pwd, centos from slave') for slave in slaves: if 'centos6' in slave.centos: # centos6直接跳过 continue mftp = MFTP(slave.ftp_host, slave.ftp_user, slave.ftp_pwd, pasv=True) try: mftp.upload_file(file_name, remote_path, local_path) print "同步文件成功" except BaseException as e: print '同步文件失败' print e finally: mftp.close() def del_file(file_name): host = '***' username = '***' password = '***' mftp = MFTP(host, username, password).ftp mftp.rmd('test_data') mftp.cwd('test_data') files = [] mftp.dir('', '.', files.append) for f in files: f = f.split(' ').pop() try: mftp.delete(f) print '删除%s成功' %f except: print '删除%s失败' %f mftp.cwd('..') mftp.close() def getInfoTest(): host = '***' username = '***' password = '***' mftp = MFTP(host, username, password, True) print mftp.getDirInfo('~') if __name__ == '__main__': local_url = 'E:/test_data' # local_url = '/root/ftp_test' down_all(local_url) # down_dir_test(local_url) # upload_file('1.txt', '', r'E:\test_data') # upload_file('1.txt', '', r'E:\test_data') # del_file('batj_yb.py') # print 'end' # getInfoTest()
Become a Linux Programmer