python文件实时同步_使用python通过ssh同步文件V2.0

这是一个使用Python编写的脚本,通过SSH协议实时同步本地和远程服务器之间的文件。脚本包含读取配置文件、设置代理、建立SSH连接、SFTP传输等功能,并且具有进度条显示。适用于需要定期或实时更新远程文件的场景。
摘要由CSDN通过智能技术生成

#!usr/bin/python

# coding: utf-8

import json

import os

import sys

import time

import paramiko

import win32api

import win32con

import stat

os.chdir(os.path.dirname(os.path.abspath(__file__)))

config_filename = "config.json"

conf_key = ["remoteDir", "localDir", "host", "port", "username", "password", "checkMinSize", "proxy", "proxyIP", "proxyPort"]

downloaded = 0

print_str = ''

def write_config_file():

default_conf = '{\n' \

'"remoteDir": "/www/",\n' \

'"localDir": "./www/",\n' \

'"host": "127.0.0.1",\n' \

'"port": 22,\n' \

'"username": "root",\n' \

'"password": "root",\n' \

'"checkMinSize": 1024,\n' \

'"proxy": true,\n' \

'"proxyIP": "127.0.0.1",\n' \

'"proxyPort": 1080\n' \

'}'

f = open(config_filename, "w")

f.write(default_conf)

def progress_bar(transferred, toBeTransferred):

percents = round(100.0 * transferred / float(toBeTransferred), 1)

sys.stdout.write('%s [%s%s] %s\r' % (print_str.encode("gbk"), '%s' % percents, '%', '%.2fMB' %

(toBeTransferred / 1024 / 1024.00)))

if percents != 100:

sys.stdout.flush()

else:

print

class Download:

# 初始化连接

def __init__(self):

self.config = {}

self.read_config_file()

self.set_proxy()

self.scp = paramiko.Transport((self.config["host"], self.config["port"]))

self.scp.connect(username=self.config["username"], password=self.config["password"])

self.sftp = paramiko.SFTPClient.from_transport(self.scp)

self.client = paramiko.SSHClient()

self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

self.client.connect(hostname=self.config["host"], port=self.config["port"], username=self.config["username"], password=self.config["password"])

stdin, stdout, stderr = self.client.exec_command(" find " + self.config["remoteDir"] + " -type f | wc -l")

self.file_num = int(stdout.read())

def read_config_file(self):

if os.path.isfile(config_filename):

try:

f = open(config_filename, "r")

config_ff = json.loads(f.read())

config = {}

for k in conf_key:

config[k] = config_ff[k]

self.config = config

except BaseException as e:

print e

win32api.MessageBox(0, "Config file error!", "ERROR", win32con.MB_ICONHAND)

exit()

else:

self.write_config_file()

win32api.MessageBox(0, "Config file missing. \rThe program has been created automatically.", "ERROR",

win32con.MB_ICONHAND)

exit()

def set_proxy(self):

if self.config["proxy"]:

import socks

import socket

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, self.config["proxyIP"], self.config["proxyPort"], False, '',

'')

socket.socket = socks.socksocket

def download_dir(self, localDir="", remoteDir=""):

global file_num

global print_str

global downloaded

if localDir == '':

localDir = self.config["localDir"]

if remoteDir == '':

remoteDir = self.config["remoteDir"]

localDir = localDir.rstrip("\\").rstrip("/")

remoteDir = remoteDir.rstrip("\\").rstrip("/")

if not os.path.exists(localDir):

os.makedirs(localDir)

files = self.sftp.listdir_attr(remoteDir)

for f in files:

localFile = os.path.join(localDir, f.filename)

remoteFile = remoteDir + "/" + f.filename

if stat.S_ISREG(f.st_mode):

downloaded += 1

try:

localSize = os.path.getsize(localFile)

except:

localSize = 0

if not os.path.isfile(localFile) or f.st_size != localSize:

print_str = (str(downloaded) + "/" + str(self.file_num) + "  ").rjust(11) + "Download:  ".rjust(12) + localFile

self.sftp.get(remoteFile, localFile, callback=progress_bar)

else:

print (str(downloaded) + "/" + str(self.file_num) + "  ").rjust(11) + "Exists:  ".rjust(12) + localFile

elif stat.S_ISDIR(f.st_mode):

self.download_dir(localDir=localFile, remoteDir=remoteFile)

pass

else:

pass

def download_file(self):

pass

if __name__ == "__main__":

d = Download()

print "Total files:" + str(d.file_num)

time.sleep(1)

d.download_dir()

d.scp.close()

print "Done!"

time.sleep(10)

使用pyinstaller打包成exe使用更方便

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值