/
Sender.py
52 lines (47 loc) · 1.71 KB
/
Sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#-*- coding:utf-8 -*-
#!/usr/bin/python
from paramiko import SSHClient
from scp import SCPClient
import os
import os.path as op
import time
import Recorder
class Sender(object):
def __init__(self,server,username,password,dest_path,from_path):
self.dest_path = dest_path
self.from_path = from_path
self.recorder = Recorder.Recorder()
self.ssh = SSHClient()
self.ssh.load_system_host_keys()
self.ssh.connect(server,username=username,password=password)
self.sftp = self.ssh.open_sftp()
self.scp = SCPClient(self.ssh.get_transport())
def send(self,file_path):
if op.exists(file_path):
file_modify_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(file_path).st_mtime))
if not self.recorder.isSynced(file_path,file_modify_time):
new_file_path = os.path.join(self.dest_path+'/',file_path.split(self.from_path+os.sep)[1])
new_file_path = new_file_path.replace('\\','/')
new_file_dir,new_file = op.split(new_file_path)
if not rexists(self.sftp,new_file_dir):
rmkdir(self.sftp,new_file_dir)
print 'uploading %s .....' % (file_path)
self.scp.put(file_path,new_file_path)
self.recorder.record(file_path,file_modify_time)
else:
pass
def rexists(sftp, path):
try:
sftp.stat(path)
except IOError, e:
if e[0] == 2:
return False
else:
return True
return True
def rmkdir(sftp,path):
if rexists(sftp,op.split(path)[0]):
sftp.mkdir(path)
else:
rmkdir(sftp,op.split(path)[0])
sftp.mkdir(path)