Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
603 lines (504 loc) · 18.8 KB
/
Copy pathserver.py
File metadata and controls
603 lines (504 loc) · 18.8 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import os
import socket
import threading
import datetime
import time
import pathlib
IP = socket.gethostbyname(socket.gethostname())
PORT = 4890
ADDR = (IP, PORT)
SIZE = 1024
FORMAT = "utf-8"
SERVER_PATH = r"server_data"
ADDRESSES = []
CONNECTIONS = []
DOWNLOADS = {}
DELETEME = []
FILENAMES = []
numDeletes = -1
totalFileSize = 0
m1 = False
#Dowload function
def download(conn: socket.socket, addr, filename, t0):
global totalFileSize
files = os.listdir(SERVER_PATH)
msg = "DONE*"
#File if requested file in server dir
if filename in files:
fileSize = str(os.path.getsize(f"{SERVER_PATH}\{filename}"))
if totalFileSize != 0:
send_data = f"DOWNLOAD*{filename}*{fileSize}*{t0}*{totalFileSize}"
conn.sendall(send_data.encode(FORMAT))
else:
send_data = f"DOWNLOAD*{filename}*{fileSize}*{t0}"
conn.sendall(send_data.encode(FORMAT))
if filename not in DOWNLOADS:
DOWNLOADS[filename] = 0
else:
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUEST*{filename}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
msg += "Requesting file"
else:
#upload from ADDRESSES[0]
send_data = f"REQUEST*{filename}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
msg += "Requesting file"
def downloadfirstm1(conn:socket.socket, addr, goodfilename, t0, badfilename):
global totalFileSize
files = os.listdir(SERVER_PATH)
msg = "DONE*"
print(f"downloadingm1 {goodfilename}")
#File if requested file in server dir
if goodfilename not in DOWNLOADS:
DOWNLOADS[goodfilename] = 0
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUESTM1*{goodfilename}*{badfilename}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
msg += "Requesting file"
else:
#upload from ADDRESSES[0]
send_data = f"REQUESTM1*{goodfilename}*{badfilename}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
msg += "Requesting file"
#Download function multiple strategy 1
def downloadm1(conn: socket.socket, addr, recieved):
filename1 = recieved[1]
filename2 = recieved[2]
t0 = recieved[3]
global m1
files = os.listdir(SERVER_PATH)
if m1:
#second run
if filename1 in FILENAMES:
download(conn, addr, filename2, t0)
else:
download(conn, addr, filename1, t0)
m1 = False
else:
#first run
if filename1 in files:
downloadfirstm1(conn, addr, filename2, t0, filename1)
else:
downloadfirstm1(conn, addr, filename1, t0, filename2)
global totalFileSize
totalFileSize = 0
#Download function multiple strategy 2
def downloadm2(conn: socket.socket, addr, recieved):
filename1 = recieved[1]
filename2 = recieved[2]
t0 = recieved[3]
files = os.listdir(SERVER_PATH)
if filename1 not in DOWNLOADS:
DOWNLOADS[filename1] = 0
if filename2 not in DOWNLOADS:
DOWNLOADS[filename2] = 0
if filename1 not in files:
DELETEME.append(filename1)
global numDeletes
numDeletes += 1
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUESTM2*{filename1}*{filename2}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
#upload from ADDRESSES[0]
send_data = f"REQUESTM2*{filename1}*{filename2}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
if filename2 not in files:
DELETEME.append(filename2)
numDeletes += 1
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUESTM2*{filename2}*{filename1}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
#upload from ADDRESSES[0]
send_data = f"REQUESTM2*{filename2}*{filename1}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
#Download function multiople strategy 3
def downloadm3(conn:socket.socket, addr, recieved):
filename1 = recieved[1]
filename2 = recieved[2]
t0 = recieved[3]
files = os.listdir(SERVER_PATH)
if filename1 not in DOWNLOADS:
DOWNLOADS[filename1] = 0
if filename2 not in DOWNLOADS:
DOWNLOADS[filename2] = 0
if filename1 not in files:
DELETEME.append(filename1)
global numDeletes
numDeletes += 1
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUESTM3*{filename1}*{filename2}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
#upload from ADDRESSES[0]
send_data = f"REQUESTM3*{filename1}*{filename2}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
if filename2 not in files:
DELETEME.append(filename2)
numDeletes += 1
if(addr == ADDRESSES[0]):
#upload from ADDRESSES[1]
send_data = f"REQUESTM3*{filename2}*{filename1}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
#upload from ADDRESSES[0]
send_data = f"REQUESTM3*{filename2}*{filename1}*{t0}"
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
#Ready, start writing
def ready(conn: socket.socket, recieved):
filename = recieved[1]
fileSize = recieved[2]
fileSize = int(fileSize)
print("got ready")
with open(f"{SERVER_PATH}\{filename}", "rb") as f:
for l in f:
conn.sendall(l)
f.close()
#Upload function
def upload(conn: socket.socket, recieved):
name = recieved[1]
filepath = os.path.join(SERVER_PATH, name)
msg = f"READY*{name}"
conn.sendall(msg.encode(FORMAT))
fileSize = conn.recv(SIZE).decode(FORMAT)
fileSize = int(fileSize)
data = bytearray()
with open(filepath,"wb") as f:
while len(data) != fileSize:
l = conn.recv(SIZE)
f.write(l)
data.extend(l)
print(f"Recieved {len(data)} bytes of {fileSize} bytes")
t0 = recieved[2]
fileSize = str(fileSize)
send_data = f"DONE*File uploaded successfully*{t0}*{fileSize}"
conn.sendall(send_data.encode(FORMAT))
#List dir function
def dir(conn: socket.socket):
files = os.listdir(SERVER_PATH)
for item in files:
if item not in DOWNLOADS:
DOWNLOADS[item] = 0
msg = "DONE*"
#If no files in DIR mark msg to empty
if len(files) == 0:
msg += "The server directory is empty"
#Else, there are files,
else:
for item in os.scandir(SERVER_PATH):
if item.name not in DOWNLOADS:
DOWNLOADS[item.name] = 0
modified = datetime.datetime.fromtimestamp(item.stat().st_atime, tz=datetime.timezone.utc)
msg += f"NAME: {item.name} SIZE: {item.stat().st_size} BYTES UPLOAD DATE AND TIME: {modified} UTC Downloads: {DOWNLOADS[item.name]}\n"
conn.sendall(msg.encode(FORMAT))
#Request function
def request(conn: socket.socket, addr, recieved):
#Recieve file
name = recieved[1]
fileSize = recieved[2]
t0 = recieved[3]
filepath = os.path.join(SERVER_PATH, name)
msg = f"READYD*{name}*{fileSize}"
conn.sendall(msg.encode(FORMAT))
fileSize = int(fileSize)
dataRecieved = bytearray()
with open(filepath, "wb") as f:
while len(dataRecieved) != fileSize:
l = conn.recv(SIZE)
f.write(l)
dataRecieved.extend(l)
print(f"Recieved {len(dataRecieved)} bytes of {fileSize} bytes")
if name not in DOWNLOADS:
DOWNLOADS[name] = 0
if(addr == ADDRESSES[0]):
#if recieved from client 0, send to client 1
send_data = f"DOWNLOADX*{name}*{fileSize}*{t0}"
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
#otherwise, we recieved the file from client 1, and client 0 needs it
send_data = f"DOWNLOADX*{name}*{fileSize}*{t0}"
#msg += "File downloaded successfully."
DOWNLOADS[name] = 0
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
def requestm1(conn:socket.socket, addr, recieved):
name = recieved[1] #good name
fileSize = recieved[2]
name2 = recieved[3] #bad name
t0 = recieved[4]
filepath = os.path.join(SERVER_PATH, name)
msg = f"READYD*{name}*{fileSize}"
conn.sendall(msg.encode(FORMAT))
fileSize = int(fileSize)
dataRecieved = bytearray()
with open(filepath, "wb") as f:
while len(dataRecieved) != fileSize:
l = conn.recv(SIZE)
f.write(l)
dataRecieved.extend(l)
print(f"Recieved {len(dataRecieved)} bytes of {fileSize} bytes")
fileSize2 = int(os.path.getsize(f"{SERVER_PATH}\{name2}"))
totalFileSize = fileSize + fileSize2
print(totalFileSize)
send_data = f"DOWNLOAD1*{name}*{fileSize}*{name2}*{t0}*{totalFileSize}"
if(addr == ADDRESSES[0]):
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
#Request multiple part 2
def requestm2(conn: socket.socket, addr, recieved):
name = recieved[1]
fileSize = recieved[2]
name2 = recieved[3]
t0 = recieved[4]
filepath = os.path.join(SERVER_PATH, name)
msg = f"READYD*{name}*{fileSize}"
conn.sendall(msg.encode(FORMAT))
fileSize = int(fileSize)
dataRecieved = bytearray()
with open(filepath, "wb") as f:
while len(dataRecieved) != fileSize:
l = conn.recv(SIZE)
f.write(l)
dataRecieved.extend(l)
print(f"Recieved {len(dataRecieved)} bytes of {fileSize} bytes")
send_data = f"FINISHM2*{name}*{name2}*{fileSize}*{t0}"
if(addr == ADDRESSES[0]):
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
#Request multiple part 3
def requestm3(conn: socket.socket, addr, recieved):
name = recieved[1]
fileSize = recieved[2]
name2 = recieved[3]
t0 = recieved[4]
filepath = os.path.join(SERVER_PATH, name)
msg = f"READYD*{name}*{fileSize}"
conn.sendall(msg.encode(FORMAT))
fileSize = int(fileSize)
dataRecieved = bytearray()
with open(filepath, "wb") as f:
while len(dataRecieved) != fileSize:
l = conn.recv(SIZE)
f.write(l)
dataRecieved.extend(l)
print(f"Recieved {len(dataRecieved)} bytes of {fileSize} bytes")
send_data = f"FINISHM3*{name}*{name2}*{fileSize}*{t0}"
if(addr == ADDRESSES[0]):
CONNECTIONS[1].sendall(send_data.encode(FORMAT))
else:
CONNECTIONS[0].sendall(send_data.encode(FORMAT))
#Finish multiple part 2
def finishm2(conn: socket.socket, recieved):
filename1,filename2 = recieved[1], recieved[2]
t0 = recieved[3]
if pathlib.Path(filename1).suffix == '.mp3' or pathlib.Path(filename1).suffix == '.txt':
with open(f"{SERVER_PATH}\{filename1}", "rb") as f:
data = f.read()
with open(f"{SERVER_PATH}\{filename2}", "rb") as f:
data2 = f.read()
data3 = data + data2
DOWNLOADS[filename1] += 1
DOWNLOADS[filename2] += 1
filename1 = filename1.split(".")
filename3 = filename1[0] +"+"+ filename2
with open(f"{SERVER_PATH}\{filename3}", "wb") as f:
f.write(data3)
else:
data1 = f"file '{filename1}'\n"
data2 = f"file '{filename2}'"
data3 = data1 + data2
with open(f"{SERVER_PATH}\mylist.txt",'w') as f:
f.write(data3)
filename1 = filename1.split(".")
filename3 = filename1[0] +"+"+ filename2
os.system(f"ffmpeg -f concat -safe 0 -i C:/Users/jimne/VisualStudioCode/CS371Project/Multithreaded-File-Transfer-using-TCP-Socket-in-Python/server_data/mylist.txt -c copy {SERVER_PATH}\\{filename3}")
os.system(f"del {SERVER_PATH}\mylist.txt")
if filename3 not in DOWNLOADS:
DOWNLOADS[filename3] = 0
fileSize = str(os.path.getsize(f"{SERVER_PATH}\{filename3}"))
send_data = f"DOWNLOADX*{filename3}*{fileSize}*{t0}"
conn.sendall(send_data.encode(FORMAT))
global numDeletes
os.system(f"del {SERVER_PATH}\{DELETEME[numDeletes]}")
#Finism multiple part 3
def finishm3(conn: socket.socket, recieved):
filename1, filename2 = recieved[1], recieved[2]
t0 = recieved[3]
fileSize1 = str(os.path.getsize(f"{SERVER_PATH}\{filename1}"))
fileSize2 = str(os.path.getsize(f"{SERVER_PATH}\{filename2}"))
global totalFileSize
totalFileSize = int(fileSize1) + int(fileSize2)
global numDeletes
if DELETEME[numDeletes] == filename1:
send_data = f"DOWNLOAD3*{filename1}*{fileSize1}*{filename2}*{fileSize2}*{t0}"
conn.sendall(send_data.encode(FORMAT))
else:
send_data = f"DOWNLOAD3*{filename2}*{fileSize2}*{filename1}*{fileSize1}*{t0}"
conn.sendall(send_data.encode(FORMAT))
#Delete function
def delete(conn: socket.socket, recieved):
files = os.listdir(SERVER_PATH)
msg = "DONE*"
filename = recieved[1]
if len(files) == 0:
msg += "The server directory is empty"
#Windows del command
else:
if filename in files:
os.system(f"del {SERVER_PATH}\{filename}")
msg += "File deleted successfully."
else:
msg += "File not found."
conn.sendall(msg.encode(FORMAT))
#Acknowledge delete after borrowing
def ackd(conn: socket.socket, recieved):
files = os.listdir(SERVER_PATH)
msg = "DONE*"
filename = recieved[1]
t0 = recieved[2]
fileSize = recieved[3]
if len(files) == 0:
msg += "The server directory is empty"
#Delete after borrowing
else:
if filename in files:
os.system(f"del {SERVER_PATH}\{filename}")
msg += f"File successfully downloaded, server cleaned.*{t0}*{fileSize}"
DOWNLOADS[filename] += 1
else:
msg += "File not found."
conn.sendall(msg.encode(FORMAT))
def ackd1(conn:socket.socket, recieved):
files = os.listdir(SERVER_PATH)
msg = "DOWNLOADM1*"
filename = recieved[1]
t0 = recieved[2]
fileSize = recieved[3]
newName = recieved[4]
global totalFileSize
global m1
totalFileSize = totalFileSize + int(fileSize)
if len(files) == 0:
msg += "The server directory is empty"
#Delete after borrowing
else:
if filename in files:
os.system(f"del {SERVER_PATH}\{filename}")
msg += f"{filename}*{newName}*{t0}"
DOWNLOADS[filename] += 1
else:
msg += "File not found."
conn.sendall(msg.encode(FORMAT))
m1 = True
FILENAMES.append(filename)
#Acknowledge and delete part 3
def ackd3(conn: socket.socket, recieved):
fileName2 = recieved[2]
fileSize2 = recieved[3]
t0 = recieved[4]
global totalFileSize
os.system(f"del {SERVER_PATH}\{DELETEME[numDeletes]}")
send_data = f"DOWNLOAD*{fileName2}*{fileSize2}*{t0}*{totalFileSize}"
conn.sendall(send_data.encode(FORMAT))
#Acknowledge
def ack(conn: socket.socket, recieved):
files = os.listdir(SERVER_PATH)
filename = recieved[1]
t0 = recieved[2]
fileSize = recieved[3]
msg = f"DONE*File downloaded successfully*{t0}*{fileSize}"
DOWNLOADS[filename] += 1
conn.sendall(msg.encode(FORMAT))
global totalFileSize
global m1
totalFileSize = 0
#Error
def err(recieved, addr):
msg = "DONE*"+recieved[1]
if addr == ADDRESSES[0]:
#if recieved error from first address, send message to second
CONNECTIONS[1].sendall(msg.encode(FORMAT))
else:
#else, recieved error from second client, so send message to first
CONNECTIONS[0].sendall(msg.encode(FORMAT))
#Listen for commands to run functions
def handle_client(conn: socket.socket, addr):
ADDRESSES.append(addr)
CONNECTIONS.append(conn)
print(f"[ CONNECTION ] {addr} connected.")
conn.send("DONE*DIR - List files in server\nUPLOAD filename - upload to path\nDELETE filename - remove file from server\nDOWNLOAD filename - Download file from server\nDOWNLOADM1 filename filename - download one filename at a time\nDOWNLOADM2 filename filename - retrieve one file from client 2 concatenate with other file in server\nDOWNLOADM3 filename filename - retrieve file from client send one at a time\nLOGOUT".encode(FORMAT))
while True:
recieved = conn.recv(SIZE).decode(FORMAT)
recieved = recieved.split("*")
cmd = recieved[0]
#List files in DIR
if cmd == "DIR":
dir(conn)
elif cmd == "UPLOAD":
upload(conn, recieved)
elif cmd == "DOWNLOAD":
download(conn, addr, recieved[1], recieved[2])
elif cmd == "DOWNLOADM1":
downloadm1(conn, addr, recieved)
elif cmd == "DOWNLOADM2":
downloadm2(conn, addr, recieved)
elif cmd == "DOWNLOADM3":
downloadm3(conn, addr, recieved)
elif cmd == "REQUEST":
request(conn, addr, recieved)
elif cmd == "REQUESTM1":
requestm1(conn,addr,recieved)
elif cmd == "REQUESTM2":
requestm2(conn, addr, recieved)
elif cmd == "REQUESTM3":
requestm3(conn, addr, recieved)
elif cmd == "DELETE":
delete(conn, recieved)
#Acknowledge then delete
elif cmd == "ACKD":
ackd(conn, recieved)
elif cmd == "ACKD1":
ackd1(conn,recieved)
#Acknowledge then delete multiple part 3
elif cmd == "ACKD3":
ackd3(conn, recieved)
elif cmd == "ACK":
ack(conn, recieved)
elif cmd == "ERR":
err(recieved, addr)
elif cmd == "READY":
ready(conn, recieved)
elif cmd == "FINISHM2":
finishm2(conn, recieved)
elif cmd == "FINISHM3":
finishm3(conn, recieved)
elif cmd == "LOGOUT":
msg = "DISCONNECTED*Disconnected from server."
conn.sendall(msg.encode(FORMAT))
break
else:
msg = "DONE*Invalid Input"
conn.sendall(msg.encode(FORMAT))
print(f"[DISCONNECT] {addr} disconnected")
conn.close()
def main():
print("[STARTING] Server is starting")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
server.listen(2)
print(f"[LISTENING] Server is listening on {IP}:{PORT}.")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
if __name__ == "__main__":
main()