复制代码 代码如下:
import ftplib, socket, os
from time import sleep, ctime
def LoginFtp(self):
ftps = ftplib.FTP()
ftps.connect(self.host,self.port)
ftps.login(self.name,self.passwd)
class LoFtp(object):
'this is ftp class example'
host = str(raw_input('host,127.0.0.1\n'))
if host == '':host = '127.0.0.1'
port = raw_input('port,21\n')
if not(port.isdigit()):port =21
name = str(raw_input('name,anonymous\n'))
if name=='':name='anonymous'
passwd = str(raw_input('password\n'))
if passwd =='':passwd=''
def ZqFtp(self,host,name,passwd,port):
self.host = host
self.name = name
self.passwd = passwd
self.port = port
def LoginFtp(self):
self.ftps = ftplib.FTP()
self.ftps.connect(self.host,self.port)
self.ftps.login(self.name,self.passwd)
self.buffer = 2048 #设置缓存大小
def ShowFtp(self):
self.LoginFtp()
self.ftps.dir('/')
dirs = str(raw_input('PLEASE INPUT DIR!\n'))
print self.ftps.dir(dirs)
def UpFtp(self):
'uploads files'
self.LoginFtp()
self.ftps.set_debuglevel(2)
filename = str(raw_input('PLEASE FILE NAME!\n'))
file_open=open(filename,'rb') #打开文件 可读即可
self.ftps.storbinary('STOR %s'% os.path.basename(filename),file_open,self.buffer)
# 上传文件
self.ftps.set_debuglevel(0)
file_open.close()
def DelFtp(self):
'Delete Files'
self.LoginFtp()
filename=str(raw_input('PLEASE DELETE FILE NAME!\n'))
self.ftps.delete(filename)
def RemoveFtp(self):
'Remove File'
self.LoginFtp()
self.ftps.set_debuglevel(2)#调试级别,0无任何信息提示
oldfile=str(raw_input('PLEASE OLD FILE NAME!\n'))
newfile=str(raw_input('PLEASE NEW FILE NAME!\n'))
self.ftps.rename(oldfile,newfile)
self.ftps.set_debuglevel(0)
def DownFtp(self):
'Download File'
self.LoginFtp()
self.ftps.set_debuglevel(2)
filename=str(raw_input('PLEASE FILE NAME!\n'))
file_down = open(filename,'wb').write
self.ftps.retrbinary('STOP %s' % os.path.basename(filename),file_down,self.buffer)
self.ftps.set_debuglevel(0)
file_down.close()
a = LoFtp()
print a.ShowFtp()
while True:
helpn= str(raw_input('Whether to continue to view or exit immediately!(y/n/q)\n'))
if (helpn=='y')or(helpn=='Y'):
dirs = str(raw_input('PLEASE INPUT DIR!\n'))
a.ftps.dir(dirs)
elif (helpn=='q')or (helpn=='Q'):
exit()
else:
break
while True:
print '上传请选择----1'
print '下载请选择----2'
print '修改FTP文件名称----3'
num = int(raw_input('PLEASE INPUT NUMBER![exit:5]\n'))
if num ==1:
upf = a.UpFtp()
print 'Upfile ok!'
elif num ==2:
dof = a.DownFtp()
print 'Download file ok!'
elif num ==3:
ref = a.RemoveFtp()
print 'Remove file ok!'
else:
a.ftps.quit()
print 'Bingo!'
break
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8