-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·69 lines (61 loc) · 1.96 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
import socket
import threading
import sys
import select
connections = [] #active clients
def new_client(con,addr):
try:
pass
# con.send('Welcome\n'.encode('utf-8'))
# broadcast(' Joined\n'.encode('utf-8'),con,addr[0])
except:
pass
while True:
try:
message = con.recv(2048) #max size of message 2048
if message:
print("(" , addr[0] , ") : " , message.decode('utf-8'))
broadcast(message,con,addr[0])
else:
remove(con) #Broken connection ==> Terminate
print("Removing : " , addr[0])
message = ' left \n'
broadcast(message.encode('utf-8'),con,addr[0])
return 0
except:
continue
def broadcast(message , con , addr):
global connections
outbox = '(' + addr +') ' + message.decode('utf-8') #Its a string
for clients in connections:
if clients != con:
try:
clients.send(outbox.encode('utf-8'))
except:
clients.close()
remove(clients)
def remove(con):
if con in connections:
connections.remove(con)
return 0
def main():
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP IPv4/6
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
IP , PORT = sys.argv[1] , int(sys.argv[2])
sock.bind((IP,PORT))
sock.listen(100) # 100 connection at a time
while True:
con , addr = sock.accept()
connections.append(con)
print(addr[0] + " Joined")
conThread = threading.Thread(target=new_client,args=(con,addr))
conThread.deamon=True #CAn close program even if Thread is running
conThread.start()
con.close()
sock.close()
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage : Script IP PORT")
exit()
print('Server Up and Running')
main()