-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·146 lines (128 loc) · 4.68 KB
/
client.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python2
import zmq
from time import sleep
from threading import Thread
import os.path
import pyinotify
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
host, port = '127.0.0.1', 43364
root = 'files'
if not os.path.exists(root):
os.makedirs(root)
context = zmq.Context()
req = context.socket(zmq.REQ)
rep = context.socket(zmq.REP)
req.connect('tcp://{}:{}'.format(host, port))
#nowatch_files = set()
nowatch_files = {}
class Watcher(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
return self.handle(event)
def process_IN_CLOSE_WRITE(self, event):
return self.handle(event)
def handle(self, event):
filename = os.path.normpath(os.path.relpath(event.pathname, '.'))
mtime = os.path.getmtime(event.pathname)
#print(nowatch_files, mtime)
if filename in nowatch_files and mtime <= nowatch_files[filename]:
#print('###########')
#nowatch_files.remove(filename)
return
filename = os.path.normpath(os.path.relpath(filename, root))
print filename
req.send_multipart(('file_updated',
filename,
str(os.path.getsize(event.pathname)),
str(mtime)))
#req.send_multipart(('file_updated',
# filename,
# str(os.path.getsize(event.pathname))))
print req.recv_multipart()
class EventHandler(FileSystemEventHandler):
events = 0
def on_moved(self, event):
def handle_move(event):
if event.is_directory:
return
self._handle_update(event.dest_path)
handle_move(event)
if event.is_directory:
for subevent in event.sub_moved_events():
handle_move(subevent)
def on_modified(self, event):
if event.is_directory:
return
self._handle_update(event.src_path)
def _handle_update(self, abs_filename):
filename = os.path.normpath(os.path.relpath(abs_filename, '.'))
#if filename in nowatch_files:
# #nowatch_files.remove(filename)
# return
filename = os.path.normpath(os.path.relpath(filename, root))
print filename
req.send_multipart(('file_updated',
filename,
str(os.path.getsize(abs_filename)),
str(os.path.getmtime(abs_filename))))
print req.recv_multipart()
manager = pyinotify.WatchManager()
notifier = pyinotify.ThreadedNotifier(manager, Watcher())
notifier.start()
mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE
manager.add_watch(root, mask, rec=True)
#observer = Observer()
#observer.schedule(EventHandler(), path=root, recursive=True)
#observer.start()
req.send_multipart(('connect',))
_, port2 = req.recv_multipart()
rep.connect('tcp://{}:{}'.format(host, port2))
while True:
msg = rep.recv_multipart()
print 'msg', msg
try:
cmd = msg[0]
if cmd == 'start_transfert':
_, filename = msg
#nowatch_files.add(filename)
rep.send_multipart(('ok',))
elif cmd == 'end_transfert':
_, filename = msg
#nowatch_files.remove(filename)
#del nowatch_files[filename]
rep.send_multipart(('ok',))
elif cmd == 'read_chunk':
_, filename, offset, size = msg
offset = int(offset)
size = int(size)
try:
if '..' in filename:
raise IOError
filename = os.path.join(root, filename)
with open(filename, 'rb') as f:
f.seek(offset)
rep.send_multipart(('ok', f.read(size)))
except:
rep.send_multipart(('ko', 'file not found', filename))
elif cmd == 'write_chunk':
_, filename, offset, chunk = msg
if '..' in filename:
raise IOError
filename = os.path.join(root, filename)
#nowatch_files.add(filename)
offset = int(offset)
dirname = os.path.dirname(filename)
mode = 'rb+'
if not os.path.exists(filename):
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
mode = 'wb+'
with open(filename, mode) as f:
f.seek(offset)
f.write(chunk)
nowatch_files[filename] = os.path.getmtime(filename)
rep.send_multipart(('ok',))
else:
rep.send_multipart(('ok',))
except:
rep.send_multipart(('ko',))