生产情况:tomcat下业务log备份,目录分多级,然后对应目录格式放到ftp上;所以,结构上 我就是一级一级目录进行判断(因为我没有找到在ftp一次判断其子目录是否存在),还有一个low点就是我没有找到怎样一次性的调用ftp的login因为现在每次判断都需要登录一下,最终功能是实现了;想着先贴出来

#!/usr/local/bin/python3.5
###Description: 上传业务log到NFS199
###Author: Danny.Deng
###DateTime: 2016-11-25
import os,sys,shutil,time,datetime,re,socket,subprocess,ftplib
##########################
backup_dir = "/dockerlogs/"
###############ip地址依赖hosts文件中的hostname解析
ip_addr = socket.gethostbyname(socket.gethostname())
ports = sorted(os.listdir(backup_dir))
yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
thirday = (datetime.datetime.now() - datetime.timedelta(days=2)).strftime("%Y%m%d")
yearday = (datetime.datetime.now() - datetime.timedelta(days=365)).strftime("%Y%m%d")
###############取log文件
pattern = re.compile(r'' + thirday + '*.log$' )
year_pattern = re.compile(r'' + yearday + '*.log$' )
#pattern = re.compile(r'' + thirday + '*.log' )
###############取服务端口列表目录
for port in (ports):
port_dir = backup_dir + port
ftp_dir = ip_addr + "/" + port
##############判断ftp上是否有对应目录,没有则创建,此次判断是两个目录 如:192.168.20.130/8000 这两级是否存在
ftp = ftplib.FTP("192.168.xxxxx")
ftp.login("syxxx","xxxx",10)
try:
ftp.mkd(ip_addr)
ftp.cwd(ip_addr)
try:
ftp.mkd(port)
ftp.quit()
except ftplib.error_perm:
ftp.quit()
except ftplib.error_perm:
ftp.cwd(ip_addr)
try:
ftp.mkd(port)
ftp.quit()
except ftplib.error_perm:
ftp.quit()
##############取端口目录下的service目录
for services in sorted(os.listdir(port_dir)):
services_dir = port_dir + "/" + services
##############判断services目录是否存在,即第三级 如:192.168.20.130/8000/yunwei
ftp = ftplib.FTP("19xxxxx")
ftp.login("sxxx","sxxxxx",10)
try:
ftp.cwd(ftp_dir)
except ftplib.error_perm:
pass
try:
ftp.mkd(services)
except ftplib.error_perm:
ftp.quit()
#############遍历service目录中的符合的文件
for file in sorted(os.listdir(services_dir)):
match = pattern.search(file)
y_match = year_pattern.search(file)
if match:
#############匹配到文件后进行 文件名 更换操作,方便上传
ftpdir_service = ftp_dir + "/" + services
#############登录到ftp上的对应目录,准备上传
ftp = ftplib.FTP("xxxxx")
ftp.login("xxxxx","xxxxxx",10)
try:
ftp.cwd(ftpdir_service)
except ftplib.error_perm:
pass
filename = services_dir + "/" + file
file_gz = file + ".gz"
#############上传压缩文件到ftp
os.environ['filename'] = str(filename)
os.system('gzip $filename')
filename_gz = filename + ".gz"
#############只读模式打开本地需要上传的文件
filename_put = open(filename_gz,'rb')
ftp.storbinary('STOR %s' % os.path.basename(filename_gz),filename_put)
os.remove(filename_gz)
#############替换文件日期,即取出一年前的文件格式进行后续的ftp.delete
year_filename = filename_gz.replace(thirday,yearday)
#############错误处理,如果文件不存在时 pass
#ftp.dir(file_gz)
try:
ftp.delete(year_filename)
except ftplib.error_perm:
pass

  

python下操作ftp上传的更多相关文章

  1. python之实现ftp上传下载代码(含错误处理)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...

  2. 【Python学习 】Python实现的FTP上传和下载功能

    一.背景 最近公司的一些自动化操作需要使用Python来实现FTP的上传和下载功能.因此参考网上的例子,撸了一段代码来实现了该功能,下面做个记录. 二.ftplib介绍 Python中默认安装的ftp ...

  3. 使用python操作FTP上传和下载

    函数释义 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import F ...

  4. windows、linux下通过ftp上传文件小脚本

    一.windows @echo off #open ip 将要上传文件的IP地址echo open IP>ftp.up #用户名echo ninic>>ftp.up #密码echo ...

  5. python网络编程--FTP上传文件示例

    1.基础版(供学习了解原理使用,low) server服务端 import socket import struct import json server = socket.socket() ip_p ...

  6. Centos 下搭建FTP上传下载服务器

    首先判断你服务器上是否安装了vsftpd 安装vsftpd #yum -y install vsftpd   安装完成之后就要重启vsftpd服务 到vsftpd的主配置文件里面 把这个改为NO 默认 ...

  7. python之路--FTP 上传视频示例

    # 服务端 import json import socket import struct server = socket.socket() server.bind(('127.0.0.1',8001 ...

  8. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  9. windows下定时利用bat脚本实现ftp上传和下载

    前言: 工作中可能会遇到以下情况,利用windows作为中转,来实现两台linux服务器的文件传输. 实现步骤: 1.FTP上传和下载的bat脚本. 脚本分为两部分:可执行bat脚本和ftp命令文件: ...

随机推荐

  1. Java 声明和访问控制(一) 数组的声明 private可以修饰类吗

    数组的声明: int []a[] = new int[4][];//是正确的 int[] array = new int[2]{1,2};//是错误的 数组的长度是不可改变的,不能通过任何方式改变大小 ...

  2. 使用IRP进行文件操作

    使用IRP进行文件操作 首先声明这个是菜鸟—我的学习日记,不是什么高深文章,高手们慎看. 一定要先感谢为技术的进步而付出辛勤汗水的人,感谢他们对技术的共享. 一个通用IRP访问文件的十六进制编辑器(开 ...

  3. php Laravel 框架 介绍及安装

    Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于 ...

  4. eMMC(KLM8G2FE3B)

     Tiny4412原理图中,eMMC是169-PIN,资料中对应内存为16/32G:而用户手册上eMMC内存为4G,对应的是153-PIN?    原理图中上标注:KLM8G2FE3B-B001_1. ...

  5. git ignore 的使用

    http://www.barretlee.com/blog/2015/09/06/set-gitignore-after-add-file/ 需要注意的 **: 如果一个 pattern 以 ** 开 ...

  6. IIS中的Application.CommonAppDataPath

    C:\ProgramData\Microsoft Corporation\Internet Information Services\7.5.7600.16385

  7. oracle core 概述

    oracle数据库系统的架构及其复杂,其提供的特性也非常的多.作为一种关系型数据库,oracle提供的基本特性: transaction concurrency read consistent 而支撑 ...

  8. [ZOJ 3623] Battle Ships

    Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is s ...

  9. Unable to Rebuild JIRA Index

    Symptoms Accessing certain JIRA pages result in: SEVERE: Internal server error com.atlassian.jira.is ...

  10. 微软开放技术发布针对 Mac 和 Linux 的更新版 Azure Node.JS SDK 和命令行工具

    发布于 2013-12-04 作者 Eduard Koller 这次为我们使用Linux 的朋友带来了更多关于部署云上虚拟机的消息.今天,微软开放技术有限公司 (MS Open Tech),想与大家分 ...