************V 0.4Beta版本************
1.修正了History功能,使得能够正确读写文件,返回正确的值
2.经过一些测试修正了一些微小容易引起崩溃的bug
3.小幅度精简了程序
#!/usr/bin/python
'''
Created on 2011-11-5
@author: zavierxu
This software is a simple FTP client, you can use it to login to your FTP server,
download , upload your files.
'''
from ftplib import FTP
import sys, os,os.path,operator
def download(handle,filename):
df = open(filename,"wb")
try:
handle.retrbinary("RETR " + filename,df.write)
except Exception:
print "Error in downloading the remote file."
return
else:
print "Successful download!"
df.close()
return
def upload(handle,filename):
uf = open(filename,"rb")
(base,ext) = os.path.splitext(filename)
try:
handle.storbinary("STOR " + filename,uf)
except Exception:
print "Successful upload."
else:
print "Successful upload."
uf.close()
return
print "This is a simple FTP Slient. You can use it to upload and download"
f=file('history.txt','a')
f.close()
showhis=raw_input ("Would you like to see the history you logged in? yes/no :")
if showhis == 'yes':
fhis = file('history.txt')
num = 0
for linehis in fhis:
num += 1
print (str(num) + '.' + linehis)
if num == 0:
print "There is no login history."
f.close()
host_name= raw_input ("Please enter the hostname.(For Example: public.sjtu.edu.cn)\nFTP address:")
fin=file('history.txt')
i=0
while 1:
linein=fin.readline()
if len(linein) == 0:
break
if host_name == linein:
i=1;
break
if i == 0:
fout=file('history.txt','a')
fout.write(host_name)
fout.close()
host_name = host_name.replace("\n","")
user = raw_input("Enter username: ")
pwd = raw_input("Enter password: ")
try: ftph = FTP(host_name)
except:
print "Host could not be resolved."
raw_input()
sys.exit()
else: pass
try:
ftph.login(user,pwd)
except Exception:
if user == "anonymous" or user == "Anonymous" and pwd == "anonymous" or pwd == "Anonymous":
print "The server does not accept anonymous requests."
raw_input()
sys.exit()
else:
print "Invalid login combination."
raw_input()
sys.exit()
else:
print "Successfully connected!\n"
print ftph.getwelcome()
path = ftph.pwd()
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
print "Press help at any time to see proper usage.\n"
while 1:
command = raw_input("FTP ]> ")
if "get " in command:
rf = command.replace("get ","")
rf = rf.replace("\n","")
download(ftph,rf)
continue
elif "put " in command:
lf = command.replace("put ","")
lf = lf.replace("\n","")
upload(ftph,lf)
ftph.close()
ftph = FTP(host_name)
ftph.login(user,pwd)
continue
elif "mkdir " in command:
mkdirname = command.replace("mkdir ","")
mkdirname = mkdirname.replace("\n","")
try: ftph.mkd(mkdirname)
except:
print "Incorrect usage."
continue
else:
print "Directory created."
continue
elif "rmdir " in command:
rmdirname = command.replace("rmdir ","")
rmdirname = rmdirname.replace("\n","")
current = ftph.pwd()
ftph.cwd(rmdirname)
allfiles = ftph.nlst()
for file in allfiles:
try:
ftph.delete(file)
except Exception:
pass
else:
pass
ftph.cwd(current)
try:
ftph.rmd(rmdirname)
except Exception:
print "All files within the directory have been deleted, but there is still another directory inside. As deleting this directory automatically goes against true FTP protocol, you must manually delete it, before you can delete the entire directory."
else:
print "Directory deleted."
continue
elif command == "pwd":
print ftph.pwd()
continue
elif command == "ls":
print ftph.dir()
continue
elif "cd " in command:
dirpath = command.replace("cd ","")
dirpath = dirpath.replace("\n","")
try:
ftph.cwd(dirpath)
if dirpath=='..':
print "Directory changed to " + ftph.pwd()
elif dirpath=='.':
print "Directory will not change."
else:
print "Directory changed to " + dirpath
except:
print "No such directory."
continue
elif command == "up":
dir = ftph.pwd()
temp = dir
index = len(dir) - 1
for i in range(index,0,-1):
if temp[i] == "/" and i != len(dir):
ftph.cwd(temp)
print "One directory back."
continue
if(operator.contains(charset,dir[i])):
temp = temp[:-1]
if temp=="/":
ftph.cwd(temp)
print "One directory back."
elif command == "rename":
fromname = raw_input("Current file name: ")
toname = raw_input("To be changed to: ")
ftph.rename(fromname,toname)
print "Successfully renamed."
continue
elif "rm " in command:
delfile = command.replace("rm ","")
delfile = delfile.replace("\n","")
ftph.delete(delfile)