Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·106 lines (96 loc) · 2.84 KB
/
Copy pathserver.py
File metadata and controls
executable file
·106 lines (96 loc) · 2.84 KB
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
#!/usr/bin/env python3
import socket
import struct
import utilities as util
import json
import os
import os.path as op
from threading import Thread
def sendFile(fname, conn, mypath, udpAddr = None):
fpath = op.join(mypath, fname)
if op.exists(fpath):
size = op.getsize(fpath)
else:
size = -1
size = struct.pack('i', size)
if udpAddr:
conn.sendto(size, udpAddr)
else:
conn.send(size)
if not op.exists(fpath):
return
perm = os.stat(fpath).st_mode
perm = struct.pack('I', perm)
if udpAddr:
conn.sendto(perm, udpAddr)
else:
conn.send(perm)
f = open(fpath, 'rb')
l = f.read(1024)
while (l):
if udpAddr:
conn.sendto(l, udpAddr)
else:
conn.send(l)
l = f.read(1024)
f.close()
def sendIndex(flag, args, conn, mypath):
table = util.listFiles(flag, args, mypath)
tosend = json.dumps(table)
conn.send(tosend.encode())
def sendHash(flag, args, conn, mypath):
table = util.listHash(flag, args, mypath)
tosend = json.dumps(table)
conn.send(tosend.encode())
def recvCommand(conn, mypath, sudp = None):
data = conn.recv(struct.calcsize('II'))
cmd, argSize = struct.unpack('II', data)
# print(cmd, argSize)
if cmd == 4:
return 'exit'
if cmd == 1: # index
arg = conn.recv(argSize).decode().split(' ')
flag = arg[0]
args = arg[1:]
# print(cmd, flag, args)
sendIndex(flag, args, conn, mypath)
elif cmd == 2: # hash
arg = conn.recv(argSize).decode().split(' ', 1)
flag = arg[0]
try:
args = arg[1]
except:
args = None
# print(cmd, flag, args)
sendHash(flag, args, conn, mypath)
elif cmd == 3: # download
arg = conn.recv(argSize).decode().split(' ', 1)
flag = arg[0]
fname = arg[1]
# print(cmd, arg.decode())
if flag == 'UDP':
data, addr = sudp.recvfrom(1024)
sendFile(fname, sudp, mypath, addr)
else:
sendFile(fname, conn, mypath)
class Server(Thread):
def __init__(self, mypath = './files_server/', port = 60000):
# , mypath = './files_server', port = 60000
Thread.__init__(self)
self.mypath = mypath
host = os.environ.get('host') or ""
self.s = socket.socket()
self.s.bind((host, port))
self.s.listen(5)
self.sudp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sudp.bind((host, port))
def run(self):
while True:
conn, addr = self.s.accept()
# print('Got connection from', addr)
if recvCommand(conn, self.mypath, self.sudp) == 'exit':
break
# print('Done sending')
conn.close()
self.s.close()
self.sudp.close()