Python脚本通过ftp协议移植文件
需求
项目需要定时移植多个客户服务器的文件到公司服务器上,确保文件定时同步和生成监控日志
机制原理
1.客户和公司服务器同时安装vpn,绕过复杂的网关,linux下使用的OpenVPN
2.服务器定时运行Python移植脚本
3.使用Python的原因,支持多线程和完善ftp类库
代码实现(Python)
Python脚本
# coding=utf-8
import os
import ftplib
import threading
import time
import pymysql
# ftp操作类
class myFtp:
# 初始化类,连接生产服务器,链接mysql
def __init__(self, host, port, user, passwd, dbCursor):
self.ftp = ftplib.FTP()
self.msgHost = host
self.dbCursor = dbCursor
self.user = user
self.passwd = passwd
self.filepath = '/'
try:
self.ftp.connect(host, port)
msgInsert(self.user, "info", "ftp连接成功", self.dbCursor)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + self.msgHost + " ftp连接成功")
except:
msgInsert(self.user, "error", "ftp连接失败", self.dbCursor)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + self.msgHost + " ftp连接失败")
# ftp错误级别
self.ftp.set_debuglevel(0)
# self.ftp.set_pasv(0)
try:
self.ftp.login(user, passwd)
self.loginSuccess = 1
msgInsert(self.user, "info", "ftp用户登录成功", self.dbCursor)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + self.msgHost + " ftp用户登录成功")
except:
self.loginSuccess = 0
msgInsert(self.user, "error", "ftp用户登录失败", self.dbCursor)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + self.msgHost + " ftp用户登录失败")
# 检查ftp是否登录成功
def isLogin(self):
# return 1
return self.loginSuccess
# 下载单个文件
def downLoadFile(self, LocalFile, RemoteFile):
curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 如果本地有文件删除
if os.path.exists(LocalFile):
os.remove(LocalFile)
# 如果本地写入
try:
buffer_size = 1024 * 1024
file_handler = open(LocalFile, 'wb')
self.ftp.retrbinary('RETR ' + RemoteFile, file_handler.write, buffer_size)
file_handler.close()
msgInsert(self.user, "info", LocalFile + "写入本地成功", self.dbCursor)
print(curTime + " " + self.msgHost + " " + LocalFile + "写入本地成功")
self.isFileSuccess = 1
except:
msgInsert(self.user, "error", LocalFile + "写入本地失败", self.dbCursor)
print(curTime + " " + self.msgHost + " " + LocalFile + "写入本地失败")
self.isFileSuccess = 0
# 删除ftp文件
if self.isFileSuccess == 1:
try:
self.ftp.delete(RemoteFile)
msgInsert(self.user, "info", self.filepath + RemoteFile + "删除ftp文件成功", self.dbCursor)
print(curTime + " " + self.msgHost + " " + self.filepath + RemoteFile + "删除ftp文件成功")
except:
msgInsert(self.user, "error", self.filepath + RemoteFile + "删除ftp文件失败", self.dbCursor)
print(curTime + " " + self.msgHost + " " + self.filepath + RemoteFile + "删除ftp文件失败")
return True
# 下载整个目录下的文件
def downLoadFileTree(self, LocalDir, RemoteDir):
if not os.path.exists(LocalDir):
curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
try:
os.makedirs(LocalDir)
msgInsert(self.user, "info", LocalDir + "本地目录创建成功", self.dbCursor)
except:
msgInsert(self.user, "error", LocalDir + "本地目录创建失败", self.dbCursor)
print(curTime + " " + self.msgHost + " " + LocalDir + "本地目录创建失败")
try:
curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.ftp.cwd(RemoteDir)
RemoteNames = self.ftp.nlst()
msgInsert(self.user, "info", self.filepath + "切换目录和读取列表成功", self.dbCursor)
print(curTime + " " + self.msgHost + " " + self.filepath + "切换目录和读取列表成功")
except:
msgInsert(self.user, "error", self.filepath + "切换目录和读取列表失败", self.dbCursor)
print(curTime + " " + self.msgHost + " " + self.filepath + "切换目录和读取列表失败")
for file in RemoteNames:
Local = os.path.join(LocalDir, file)
# print(file)
if file.find(".") == -1:
self.filepath += file + '/'
if not os.path.exists(Local):
try:
os.makedirs(Local)
msgInsert(self.user, "info", Local + "本地目录创建成功", self.dbCursor)
print(curTime + " " + self.msgHost + " " + Local + "本地目录创建成功")
except:
msgInsert(self.user, "error", Local + "本地目录创建失败", self.dbCursor)
print(curTime + " " + self.msgHost + " " + Local + "本地目录创建失败")
self.downLoadFileTree(Local, file)
# 删除目录
self.ftp.rmd(file)
elif file.find(".") == 0:
pass
else:
self.downLoadFile(Local, file)
self.ftp.cwd("..")
# print('start')
# print(self.filepath)
self.filepath = self.filepath[0:self.filepath.rstrip("/").rfind("/")+1]
# print(self.filepath)
# print('end')
return
def close(self):
self.ftp.quit()
# 移植函数
def getSiteFtpFile(host, port, username, passwd, localDir, dbconn):
# dbconn = sqlite3.connect('getFile.sqlite3', check_same_thread=False)
try:
dbconn = pymysql.connect("127.0.0.1", "root", "root", "ftpwork")
dbCursor = dbconn.cursor()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + host + " 连接mysql成功")
except:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + host + " 连接mysql失败")
ftp = myFtp(host, port, username, passwd, dbCursor)
isLogin = ftp.isLogin()
if isLogin == 1:
try:
ftp.downLoadFileTree(localDir, '/')
except:
msgInsert(username, "error", "ftp.downLoadFileTree方法执行中断", dbCursor)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " " + host + " ftp.downLoadFileTree方法执行中断")
ftp.close()
print(time.strftime("########### " + "%Y-%m-%d %H:%M:%S", time.localtime()) + " " + host + " 执行完成 ###########")
# dbCursor.close()
dbconn.commit()
dbconn.close()
# 自定义线程
class myThread(threading.Thread):
def __init__(self, threadID, name, host, port, username, passwd, localDir, dbConn):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.host = host
self.port = port
self.username = username
self.passwd = passwd
self.localDir = localDir
self.dbConn = dbConn
def run(self):
print("开始线程:" + self.name)
getSiteFtpFile(self.host, self.port, self.username, self.passwd, self.localDir, self.dbConn)
print("退出线程:" + self.name)
# 监控消息写入mysql
def msgInsert(msgHost, msgType, msgContent, dbCur):
datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
dbCur.execute("INSERT INTO ftp_getfile (title,content,w_time,msgtype) \
VALUES ('" + msgHost + "', '" + msgContent.replace("\\","/") + "','" + datetime + "' ,'" + msgType + "')")
if __name__ == "__main__":
conn = ''
# 创建两个线程
thread1 = myThread(1, "Thread-1", "ftp_ip", 21, 'ftp_user', 'ftp_passwd', 'Z:/', conn)
# thread2 = myThread(2, "Thread-2", "ftp_ip", 21, 'ftp_user', 'ftp_passwd', "Z:/", conn)
# thread3 = myThread(3, "Thread-3", "ftp_ip", 21, 'ftp_user', 'ftp_passwd', 'D:/root-python/test/03', conn)
# thread4 = myThread(4, "Thread-4", "ftp_ip", 82, 'ftp_user', 'ftp_passwd', 'D:/root-python/test/04', conn)
# thread5 = myThread(5, "Thread-5", "ftp_ip", 21, 'ftp_user', 'ftp_passwd', 'D:/root-python/test/05', conn)
# thread6 = myThread(6, "Thread-6", "ftp_ip", 82, 'ftp_user', 'ftp_passwd', 'D:/root-python/test/06', conn)
try:
# 开启新线程
thread1.start()
# thread2.start()
# thread3.start()
# thread4.start()
# thread5.start()
# thread6.start()
thread1.join()
# thread2.join()
# thread3.join()
# thread4.join()
# thread5.join()
# thread6.join()
except:
print("Error: unable to start thread")
# while 1:
# pass
Python脚本通过ftp协议移植文件的更多相关文章
- 一个ftp协议传输文件之后执行脚本无法工作的情况
作者:良知犹存 转载授权以及围观:欢迎添加微信号:Conscience_Remains 总述 移植一个文件系统时候,我在window下git clone了对方仓库源码,然后用FileZ ...
- vsftpd-基于ftp协议的文件传输服务器软件
第一部分:在Linux上部署vsftpd服务 1. vsftpd简介 1.1 vsftpd是什么? ftp(File Transfer Protocol)文件传输协议.(实现不同操作系统之间文件的传输 ...
- Shell脚本调用ftp上传文件
Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...
- Python脚本打包成exe执行文件
需求 一个教辅目录结构检查工具,目录结构是[书籍]-[章节]-[题目|答案]-[*.jpg],后台有个异步处理的服务,需要强依赖这个目录结构. 书籍解析是单独的pipeline,日志对用户不可见,这里 ...
- 开发错误日志之FTP协议传输文件问题
从开发端用FTP协议向服务器(Linux系统)传输文件时,cat -A查询文件内容中行尾会有^M出现. 解决方案:改用SFTP协议上传文件.
- linux脚本:ftp自动传输文件
使用Shell脚本实现ftp的自动上传下载 http://liwenge.iteye.com/blog/566515 open 192.168.1.171 user guest 123456cd /h ...
- ubuntu下把python脚本转为二进制字节码文件
ubuntu下把python脚本转为二进制字节码文件 听语音 原创 | 浏览:354 | 更新:2017-12-22 14:48 1 2 3 4 5 6 7 分步阅读 自己拥有个几个python脚本文 ...
- python网络编程--FTP上传文件示例
1.基础版(供学习了解原理使用,low) server服务端 import socket import struct import json server = socket.socket() ip_p ...
- python脚本-excel批量转换为csv文件
pandas和SQL数据分析实战视频教程 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2& ...
随机推荐
- 【Ubuntu】16.04网卡信息配置
①查看操作系统版本信息: ②查看网卡信息 输入ifconfig命令查看网卡信息,下图enp0s25就是网卡名称. ifconfig ③配置网卡文件 注意:不同的linux系统,网卡配置文件是不同的,这 ...
- mapper的配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...
- CentOS系统将UTC时间修改为CST时间方法
世界协调时间(Universal Time Coordinated,UTC): GPS 系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,地方时为本地 ...
- Visio常规图表
包含的就是一些形状模块 比如框图就包含了“方块”以及“具有凸起效果的块”两个形状模版 打开visio 新建的时候选择常规类别 具有透视效果的框图 下面是基本操作: 这是自动调整大小的框 不能调整大小 ...
- 【一起学源码-微服务】Nexflix Eureka 源码八:EurekaClient注册表抓取 精妙设计分析!
前言 前情回顾 上一讲 我们通过单元测试 来梳理了EurekaClient是如何注册到server端,以及server端接收到请求是如何处理的,这里最重要的关注点是注册表的一个数据结构:Concurr ...
- Linux 安装 Nvidia 驱动出现的黑屏各种问题和解决方式
之前因为想OBS支持h264-nvenc这个功能然后就编译ffmpeg,然后使用Github上面的一个编译项目),项目编译完成之后重启电脑,然后就进入不了系统的登录页面了,选择进入Linux系统之后就 ...
- 一天入门 Python 的一些心得
1. 前言 好久没写文了.最近在搞一些好玩的技术用到了 Python .我原以为要花些时日,谁知道第一天入门之后便没有再刻意地去学习它了.这里就写写其中的一些关键点吧.如果我去学一门语言不是因为它火了 ...
- 【题解】地精部落(DP)
[题解]地精部落(DP) 设\(f_i\)表示强制第一个是谷的合法方案数 转移枚举一个排列的最大值在哪里,就把序列分成了互不相干的两个部分,把其中\(i-1\choose j-1\)的数字分配给前面部 ...
- Python数据分析:手把手教你用Pandas生成可视化图表
大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂,想要搞明白并非易事.但自从Python进入3.0时代以后, ...
- 机器学习之路--Python
常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...