./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,如需转载请自行联系原博主。
# -*- 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)