py ftp

简介:
./pyftp_sync.py /tongbu/day/spbusinessinfo/all/$mydate-walkman.000 $mydir$mydate-walkman.000 218.205.XXX.XXX Name Pass123 get >> pyftp_sync.log
# !/usr/bin/python
#
 -*- coding: utf-8 -*-
import  ftplib
import  datetime
import  os
import  sys
import  socket
import  time
# time access
TF0 = ' %Y-%m-%d-%H:%M:%S '
MAX_RETRY
= 60
SLEEP_TIME
= 30
#
#
虏脦媒潞
#
脦录镁卤戮碌氐脴路
#
脦录镁ftp目碌牡脴路
#
ftp ip
#
ftp脫禄搂脙
#
ftp脙脗
#

# cal src file path
dpath = sys.argv[ 1 ]
print   " [ "   +  datetime.datetime.now().strftime(TF0) + " ] -  " + " dpath= " + dpath
# cal desc file path
spath = sys.argv[ 2 ]
ftp_addr
= sys.argv[ 3 ]
ftp_usr
= sys.argv[ 4 ]
ftp_pwd
= sys.argv[ 5 ]
ftp_method
= sys.argv[ 6 ]
print   " [ "   +  datetime.datetime.now().strftime(TF0) + " ] -  " + " spath= " + spath
print   " [ "   +  datetime.datetime.now().strftime(TF0) + " ] -  " + " ftp= " + ftp_addr + " ,user= " + ftp_usr + " ,pwd= " + ftp_pwd


if  ftp_method  ==   ' put '   and  os.path.isfile(spath)  is  False:
        
print   " [ "   +  datetime.datetime.now().strftime(TF0) + " ] - [ERROR]  " + " spath= " + spath + "  not exists "
else :

        
for  i  in  range(MAX_RETRY):
                
try :
                        
# ftp connection
                        ftp  =  ftplib.FTP()
                        ftp.set_debuglevel(
2 )
                        ftp.connect(ftp_addr,
21 )
                        ftp.login(ftp_usr, ftp_pwd)
                        
# ftp.mkd("test1")
                         if  ftp_method  ==   ' put ' :
                            
print   " put "
                            ftp.storbinary(
" STOR  "   +  dpath, open(spath))
                        
if  ftp_method  ==   ' get ' :
                            
print   " get "
                            
# ftp.set_pasv(True);
                            ftp.retrbinary( " RETR  "   +  dpath, open(spath, ' wb ' ).write)
                        
# ftp.storlines("STOR " + dpath, open(spath))

                        
# check file
                        sfile_size = os.path.getsize(spath)
                        
print   " [%s] - file size=%.3fK "   %  (datetime.datetime.now().strftime(TF0), sfile_size / 1024 )
                        dfile_size
= ftp.size(dpath)
                        
if  sfile_size  ==  dfile_size:
                                
print   " [ "   +  datetime.datetime.now().strftime(TF0) + " ] -  " + " send file success "
                                ftp.quit()
                                ftp.close()
                                
break
                        
print   " [%s] - failed on check: src size.%d != desc size%d "   %  (datetime.datetime.now().strftime(TF0),sfile_size,dfile_size)
                        ftp.quit()
                        ftp.close()
                
except  Exception, myError:
                        excType, excValue, traceBack 
=  sys.exc_info()
                        
print  excType
                        
print  myError
                        
# print excValue
                         # print traceBack
                time.sleep(SLEEP_TIME)

本文转自博客园刘凯毅的博客,原文链接:py ftp,如需转载请自行联系原博主。
目录
相关文章
|
Shell 数据安全/隐私保护 网络安全
shell使用lftp连接ftp和sftp,并可以指定私钥
lftp连接ftp在脚本中可以 lftp -c "open username:password@host:port; ls /Friso/20180822/click/mobile/SUCCESS | wc -l" lftp usename:password@host:port -e "ls /Fr...
3847 0
|
Python
|
数据安全/隐私保护
|
监控 测试技术 Linux
|
Unix Shell 网络协议
|
数据安全/隐私保护 Python