自动更新代码到FTP
if(isChineseWord(filename)):
if not isinstance(g_filepath filename, unicode):
fullFileName = unicode(g_filepath filename, "utf-8")
file_handler = open(fullFileName, 'rb')
# 以读模式在本地打开文件
new_fullname = filename.split('.')[0] '_' str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) '.' \
filename.split('.')[1]
ftp.storbinary('STOR ' new_fullname.decode("utf8").encode('gbk'), file_handler, bufsize)
else:
file_handler = open(g_filepath filename, 'rb')
# 以读模式在本地打开文件
new_fullname = filename.split('.')[0] '_' str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) '.' \
filename.split('.')[1]
ftp.storbinary('STOR ' new_fullname, file_handler, bufsize)
def ftp_up(filename="versionLocal.txt"):
try:
ftp = ftplib.FTP()
except (socket.error, socket.gaierror):
print("ERROR: cannot reach %s" % FTP_SERVER)
return None
# 打开调试级别2,显示详细信息;0为关闭调试信息
ftp.set_debuglevel(2)
try:
ftp.connect(FTP_SERVER, PORT)
ftp.login(USER, PWD)
#ftp.encoding = 'utf-8'
except ftplib.error_perm:
print "ERROR: cannot login anonymously"
ftp.quit()
return None
ftp.set_pasv(False) # 允许或者禁用被动模式
# 登录,如果匿名登录则用空串代替即可
# print ftp.getwelcome()
# 显示ftp服务器欢迎信息
ftp.cwd(FTP_PATH)
# 选择操作目录
bufsize = 2048
# 设置缓冲块大小
if(isChineseWord(filename)):
if not isinstance(g_filepath filename, unicode):
fullFileName = unicode(g_filepath filename, "utf-8")
file_handler = open(fullFileName, 'rb')
# 以读模式在本地打开文件
new_fullname = filename.split('.')[0] '_' str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) '.' \
filename.split('.')[1]
ftp.storbinary('STOR ' new_fullname.decode("utf8").encode('gbk'), file_handler, bufsize)
else:
file_handler = open(g_filepath filename, 'rb')
# 以读模式在本地打开文件
new_fullname = filename.split('.')[0] '_' str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) '.' \
filename.split('.')[1]
ftp.storbinary('STOR ' new_fullname, file_handler, bufsize)
#ftp.storbinary('STOR %s' % os.path.basename(filename), file_handler.read(), bufsize)
# 上传文件
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
print "ftp up OK"
评论