-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathserver.py
More file actions
41 lines (29 loc) · 1.06 KB
/
server.py
File metadata and controls
41 lines (29 loc) · 1.06 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
import socket, threading, pickle
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
PORT = 5003
SERVER = socket.gethostbyname(socket.gethostname())
s.bind((SERVER, PORT))
users = []
#Function to send message to all connected clients
def client(client_socket, addr, USER):
while True:
try:
msg = client_socket.recv(1024)
user , message = pickle.loads(msg)
for client in users:
client.send(pickle.dumps((user,message)))
except:
print("Brute force close")
def threads():
s.listen()
while True:
try:
client_socket, addr = s.accept()
user = client_socket.recv(1024).decode('utf-8')
users.append(client_socket)
thread = threading.Thread(target=client, args=(client_socket, addr, user))
thread.start()
except:
s.close()
print("\033[1;32m[STARTING] Server!\033[0m \033[;1m")
threads()