-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
87 lines (71 loc) · 2.05 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
import socket
import os
import threading as thr
from datetime import datetime
# Environment Variables to the Socket
HOST = os.getenv('SOCKET_HOST', '127.0.0.1')
PORT = int(os.getenv('SOCKET_PORT', 5000))
CONNECTIONS = int(os.getenv('SOCKET_CONNECTIONS', 100))
# Function to connect to the server
def connection():
if CONNECTIONS == 0:
raise Exception("Connections number must be greater than 0!")
try:
# Create a server socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(CONNECTIONS)
except ValueError as err:
raise Exception(err)
return server
# Function to receive messages from a connection
def operator(conn, addr, clientList):
conn.send(str.encode("Bem-vindo ao Chat de Bruno e Johann!"))
conn.settimeout(None)
print("New Connection: " + addr[0] + ':' + str(addr[1]))
while True:
try:
# Receive a message from client
message = conn.recv(1024).decode()
if message:
# Get current date
now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
messageSend = "< {} > {}".format(now, message)
print (messageSend)
# Send message to users in chat
for client in clientList:
try:
client.send(messageSend.encode())
except:
client.close()
if conn in clientList:
clientList.remove(conn)
else:
if conn in clientList:
clientList.remove(conn)
break
except:
continue
conn.close()
# Function to create a user
def newUser(clientList, server, threadList):
conn, addr = server.accept()
clientList.append(conn)
# Create a new Thread
t = thr.Thread(target = operator, args=(conn, addr, clientList))
threadList.append(t)
t.start()
return clientList
# Main function
def main():
threadList = []
clientList = []
server = connection()
while 1:
clientList = newUser(clientList, server, threadList)
# Stop all threds
for t in threadList:
t.join()
server.close()
if __name__ == "__main__":
main()