-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
103 lines (77 loc) · 2.39 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
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
import socket
import threading
import os
HEADER = 64
PORT = 12345
FORMAT = 'utf-8'
DISCONNECT = 'DISCONNECT'
#SERVER = str(socket.gethostbyname(socket.gethostname()))
#SERVER = '127.0.0.1'
#SERVER = '119.160.97.67'
SERVER = ''
ADDR = (SERVER,PORT)
server= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('[SERVER]: Starting Server...')
server.bind(ADDR)
def start_server():
print('Server address : ',ADDR)
print(print(FORMAT))
server.listen()
print('waiting for connections..')
while True:
conn,addr = server.accept()
try:
t = threading.Thread(target=send_file_menu,args=(conn,addr))
t.start()
print(f'[ CONNECTION ESTABLISHED ]: {addr} connected with server.')
print(f'[CONNECTIONS COUNT]: {threading.activeCount()-1} ')
except:
print('[ EXCEPTION ] : Exception occured while establishing connection.')
#
def files_str(files):
s =''
for f in files:
s+=f+'\n'
return s
def send_file(conn,addr,path):
print(f'[ FILE TRANSFER ] {addr} requested for file "{path}" ')
f = open(f'server\{path}','r')
file = f.read()
file_len = str(len(file))
file_len += ' '* (HEADER - len(file_len))
print('[ STATUS ] : sending file...')
#print(file)
print('\n',len(file))
# sending file length as header
conn.send(bytes(file_len,FORMAT))
# sending complete file
conn.send(bytes(file,FORMAT))
print(f'[ COMPLETED ] : file "{path}" sent successfully to {addr}.')
def send_file_menu(conn,addr):
files = os.listdir('server\\')
#sending header for msg length
msg = files_str(files)
send(msg,conn)
filename = recv(conn)
if filename:
send_file(conn,addr,filename)
msg = recv(conn)
if msg ==DISCONNECT:
conn.close()
print(f'[CLOSED]: Connection with {addr} closed.')
print(f'[CONNECTIONS COUNT]: {threading.activeCount()-1} ')
# for sending messages to clients
def send(msg,conn):
msg_len = str(len(msg))
msg_len += ' '* (HEADER - len(msg))
# sending message length
conn.send(bytes(msg_len,FORMAT))
# sending real message
conn.send(bytes(msg,FORMAT))
# for receiving messages from clients
def recv(conn):
msg_len = conn.recv(HEADER).decode(FORMAT)
if msg_len:
msg = conn.recv(int(msg_len)).decode(FORMAT)
return msg
start_server()