-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (56 loc) · 2.59 KB
/
server.py
File metadata and controls
65 lines (56 loc) · 2.59 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import socket
import threading
from os import system
from datetime import datetime
HEADER = 64
PORT = 65432
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
VERSION = "Server"
DEVELOPER = "github.com/mattiastofte/"
clients = []
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
system("title "+VERSION)
def handle_client(conn, addr):
clients.append(conn)
connected = True
while connected:
try:
message_length = conn.recv(HEADER).decode(FORMAT) # decode from bytes format to string using utf-8
if message_length: # first message is empty (null), to tell that the client is connected, therefor you have to check if message_length has content.
message_length = int(message_length)
message = conn.recv(message_length).decode(FORMAT) # blocking line of code, wont pass before we recieve message
if message == DISCONNECT_MESSAGE:
connected = False
print(f"[{get_time()}] [{addr[1]}] {message}")
sendallclients((f"[{get_time()}] [#{addr[1]}] {message}").encode(FORMAT))
except:
print(f"[{get_time()}] [Server] Client with id #{addr[1]} has disconnected from the server.")
print(f"[{get_time()}] [Server] {threading.activeCount()-2} client(s) currently connected.")
break
conn.close()
def sendallclients(message):
message_length = len(message)
send_length = str(message_length).encode(FORMAT) # sends a string with length of message
send_length += b' '*(HEADER-len(send_length))
for client in clients:
client.send(send_length)
client.send(message)
def get_time():
return datetime.now().strftime("%H:%M")
def start():
server.listen()
print(f"[Server] Listening on {SERVER}\n")
while True:
conn, addr = server.accept() # waits for new connection, stores object in conn and address in addr
thread = threading.Thread(target=handle_client, args=(conn, addr)) # new thread with function handle_client as target and conn object and addr as arguments
thread.start() # starts new thread
print(f"[{get_time()}] [Server] Client with id #{addr[1]} has connected to the server.")
print(f"[{get_time()}] [Server] {threading.activeCount()-1} client(s) currently connected.")
print(f"simple-chat\nmade with <3 by {DEVELOPER}")
print("━━━━━━━━━━━━━━━━━━━━━━━━\n")
print(f"\n[Server] Socket created succesfully. Starting server.")
start()