-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
57 lines (48 loc) · 1.74 KB
/
server.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
import socket
import tqdm
import os
from threading import Thread
from socketserver import ThreadingMixIn
TCP_IP = socket.gethostbyaddr("18.189.170.225")[0]
TCP_PORT = 60001
buffer = 1024
print (TCP_IP,":",TCP_PORT)
class ClientThread(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
def run(self):
conn, addr = tcpsock.accept()
received = conn.recv(buffer).decode()
filename, filesize = received.split(" ")
# if such a file already exists, it would be removed by this command
filename = os.path.basename(filename)
progress = tqdm.tqdm(range(int(filesize)), f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "wb") as f:
for _ in progress:
# read 1024 bytes from the socket (receive)
bytes_read = tcpsock.recv(buffer)
if not bytes_read:
# nothing is received
# file transmitting is done
break
# write to the file the bytes we just received
f.write(bytes_read)
# update the progress bar
progress.update(len(bytes_read))
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print ("Waiting for incoming connections...")
(conn, (ip,port)) = tcpsock.accept()
print ('Got connection from ', (ip,port))
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()