什么都不說,在代碼里
#!/usr/bin/env python
#-*- encoding: utf8 -*-
from ftplib import FTP
import sys,os,getopt
opts,args=getopt.getopt(sys.argv[1:],'hf:d:i:u:p:')
def usage():
print '''
Help Information:
上傳正常結束后,會在上次文件邊創建成功狀態文件夾,名稱為 [上次文件名.state ]
-h : Show help information
-f : local upload file eg -> /home/user/xx/file.tar
-d : upload to ftp path eg -> /x/xx/xxx
-i : [optional] Default 122.102.xx.xx
-u : [optional] Default xx
-p : [optional] Default *** (xx passwd)
'''
fip='122.xx.xx.xx'
fur='xx'
fpw='123'
for o,a in opts:
if o=='-h':
usage()
sys.exit()
if o=='-f' : upload_file=a
if o=='-d' : ftp_path=a
if o=='-i' : fip=a
if o=='-u' : fur=a
if o=='-p' : fpw=a
ftp = FTP(fip)
ftp.login(fur,fpw)
if not ( locals().has_key('ftp_path') and locals().has_key('upload_file') ):
usage()
sys.exit()
# 迭代 創建 目錄
to_path='/'
for sp in ftp_path.split('/')[1:]:
drs = ftp.nlst(to_path)
if to_path=='/':to_path+=sp
else : to_path+='/'+sp
if not to_path in drs :
ftp.mkd(to_path)
#到 最終 目錄下
ftp.cwd(to_path)
# 上傳準備
bufsize = 1024
file_handler = open(upload_file,'rb')
file_name=os.path.split(upload_file)[1]
# 判定是否 有 上傳完 狀態文件夾,如果有刪除
sfile=to_path+'/'+file_name+'.state'
if sfile in ftp.nlst(to_path):
print '[Resend] delete original dir state '+sfile
ftp.rmd( sfile )
# 上傳文件
ftp.storbinary('STOR %s' % (file_name),file_handler,bufsize)
# 如果 上傳 文件大小不一 ,不標注 成功 上傳 狀態文件夾
if not os.path.getsize(upload_file) == ftp.size(to_path+'/'+file_name) :
print '[Error] upload to ftp size Different ! '
sys.exit()
# 上傳 成功 創建標示狀態 文件夾
ftp.mkd(sfile)
file_handler.close()
ftp.quit()
整理 m.tkk7.com/Good-Game