用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[0])=='quit':
break
else:
actions[string.lower(cmd[0])]
elif len(cmd)==2:
actions[string.lower(cmd[0])](cmd[1])
elif len(cmd)==3:
actions[string.lower(cmd[0])](cmd[1],cmd[2])
else:
print 'type error'
except:
print 'command error'
ftp.quit()
随后我会写一个用Tkinter做的图形界面的版本