oulndpsrxf 发表于 2016-6-7 11:54:59

python写的FTP工具

用Python写的FTP工具

import string
from ftplib import FTP
bufsize=1024
def Get(filename):
command='RETR '+filename
ftp.retrbinary(command,open(filename,'wb').write,bufsize)
print 'download successfully'
def Put(filename):
command='STOR '+filename
filehandler=open(filename,'rb')
ftp.storbinary(command,filehandler,bufsize)
filehandler.close()
print 'upload successfully'
def PWD():
print ftp.pwd()
def Size(filename):
print ftp.size(filename)
def Help():
print '''
==============================
Simple Python FTP
==============================
cd       enter document
delete   delete file
dir      get files list
get      download file
help   help
mkdir    create document
put      upload file
pwd      get current path
rename   rename file name
rmdir    delete document
size   get file size
'''
server=raw_input('enter FTP server info: ')
ftp=FTP(server)
username=raw_input('username:')
password=raw_input('password')
ftp.login(username,password)
print ftp.getwelcome()
actions={'dir':ftp.dir,'pwd':PWD,'cd':ftp.cwd,'get':Get,
'put':Put,'help':Help,'rmdir':ftp.rmd,
'mkdir':ftp.mkd,'delete':ftp.delete,
'size':Size,'rename':ftp.rename}
while True:
print 'pyftp',
cmds=raw_input
cmd=string.split(cmds)
try:
if len(cmd)==1:
if string.lower(cmd)=='quit':
break
else:
actions)]
elif len(cmd)==2:
actions)](cmd)
elif len(cmd)==3:
actions)](cmd,cmd)
else:
print 'type error'
except:
print 'command error'
ftp.quit()

随后我会写一个用Tkinter做的图形界面的版本
页: [1]
查看完整版本: python写的FTP工具