collab/mm20
git clone https://git.julia.blue/collab/mm20
5ac4b16
telemetry
1import socket 2import threading 3import random 4 5 6HEADER = 64 7PORT = 65432 8SERVER = socket .gethostbyname (socket .gethostname ()) 9ADDR = (SERVER ,PORT ) 10FORMAT = 'utf-8' 11DISCONNECT_MESSAGE = '!DISCONNECT' 12 13 14server = socket .socket (socket .AF_INET ,socket .SOCK_STREAM ) 15server .bind (ADDR ) 16 17def handle_client (conn ,addr ):#handles client individually (threaded) 18f"[NEW CONNECTION] { addr } connected." ) 19 20connected = True 21while connected : 22msg_length = conn .recv (HEADER ).decode (FORMAT )#from client 23if msg_length :#if msg_length has something 24msg_length = int (msg_length ) 25msg = conn .recv (msg_length ).decode (FORMAT ) 26 27if msg == DISCONNECT_MESSAGE : 28connected = False 29 30f'[ { addr } ] { msg } ' )#prints what client sent to server 31 32 33conn .send (f"[FROM SERVER] { msg } { random . randint ( 1 , 10 ) } " .encode (FORMAT )) 34 35#for debugging 36# voltage = random.randint(1,10) 37# conn.send(f'[FROM SERVER] Voltage is now {voltage}'.encode(FORMAT)) 38 39 40conn .close () 41 42 43def start (): 44server .listen () 45f"[LISTENING] Server is listening on { SERVER } " ) 46while True : 47conn ,addr = server .accept () 48thread = threading .Thread (target = handle_client ,args = (conn ,addr )) 49thread .start () 50f"[ACTIVE CONNECTIONS] { threading . activeCount () - 1 } " ) 51 52if __name__ == '__main__' : 53"[STARTING] server is starting..." ) 54start () 55 56 57# def maingui(client): 58# pass 59# 60# guithread = threading.Thread(target = maingui, args = (client)) 61# guithread.start() 62# 63# def livefeed(timeout): 64# pass 65# 66# livefeedthread = threading.Thread(target = livefeed, args = (timeout)) 67# livefeedthread.start()