-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
74 lines (58 loc) · 1.88 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
import socket
import threading
import sys
import Banner
import getIp
print(Banner.banner)
ipAddress = getIp.get_network_ip()
isDebug = input("Do you want to debug output Y/n? ")
port = int(input("What port do you want to listen on? "))
def handle_client(client, clients):
while True:
try:
# Receive message
msgRecv = client.recv(1000000)
# Break if no message received
if not msgRecv:
break
# Decode the message received
msg = msgRecv.decode('utf-8')
if isDebug.lower() == "y" or "yes":
print("Message Received is: ", msg)
# Send the message to all clients connected
if len(clients) > 1:
for otherClient in clients:
otherClient.send(msg.encode('utf-8'))
print("------------------- New Message -------------------")
except Exception as e:
if isDebug == "Y" or "y" or "yes":
print(f"Error handling client: {str(e)}")
break
# Remove the client from the list
clients.remove(client)
client.close()
def SimpleTCPServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ""
s.bind((host, port))
s.listen()
clients = []
nums = [1, 2, 4]
print(Banner.style.RESET)
print(
f"""
🖥 Server has started.
🚀 Listening on {Banner.style.CYAN}{ ipAddress}:{port}...
""")
print(Banner.style.GREEN)
while True:
c, addr = s.accept()
clients.append(c)
if isDebug == "Y" or "y" or "yes":
print(f"Got connection from {addr}")
# Start a new thread to handle the client
client_thread = threading.Thread(
target=handle_client, args=(c, clients))
client_thread.start()
if __name__ == "__main__":
SimpleTCPServer()