-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_2.py
69 lines (49 loc) · 1.64 KB
/
server_2.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
import socket as s
import os
import threading
import tqdm
PORT = 12345
SERVER_IP_ADDRESS='0.0.0.0'
BUFFER_SIZE =4096
def start_server():
client = s.socket()
print('Waiting for connections...')
client.bind((SERVER_IP_ADDRESS,PORT))
client.listen(5)
while True:
print('[STATUS]: Waiting for connections.')
con,addr=client.accept()
try:
t=threading.Thread(target=handle_connection,args=(con,addr))
t.start()
print(f'[ + ]: {addr} connected.')
print(f'[ COUNT ] : thread count = {threading.active_count()}')
except:
print(f'[ EXCEPTION ] : ERROR in connection to "{addr}" ')
def handle_connection(con,addr):
send_file_menu(con,addr)
file_name = con.recv(BUFFER_SIZE).decode()
print(f'[ DOWNLOAD ] : file "{file_name}" requested by "{addr}" for downloading')
file_size=os.path.getsize(f'server/{file_name}')
progress = tqdm.tqdm(range(file_size), f"Sending {file_name}", unit="B", unit_scale=True, unit_divisor=1024)
with open(f'server/{file_name}','rb') as f:
while True:
data = f.read(BUFFER_SIZE)
if not data :
break
con.sendall(data)
progress.update(len(data))
print('')
con.close()
#print(f'{len(data)} sent...')
#con.send('Welcome to connection.'.encode())
def files_str(files):
s =''
for f in files:
s+=f+'\n'
return s
def send_file_menu(con,addr):
files = os.listdir(os.path.join(os.getcwd(),'server'))
msg = files_str(files)
con.send(msg.encode())
start_server()