diff --git a/server/server.py b/server/server.py index ae1c10c..d6f604f 100644 --- a/server/server.py +++ b/server/server.py @@ -1,5 +1,10 @@ import socket import ws2801 +import re + +def validate_data(data): + reg = re.compile("[0-9a-f]{6}$") + return reg.match(data) def start(): host = "0.0.0.0" @@ -10,14 +15,18 @@ def start(): # configure how many client the server can listen simultaneously server_socket.listen(1) - conn, address = server_socket.accept() # accept new connection - print("Connection from: " + str(address)) while True: + conn, address = server_socket.accept() # accept new connection + print("Connection from: " + str(address)) # receive data stream. it won't accept data packet greater than 1024 bytes data = conn.recv(1024).decode() - r,g,b = ws2801.hex_to_rgb(data) - ws2801.set_color(r,g,b) - + if not data: + print("Disconnected: " + str(address)) + break + if validate_data(data): + r,g,b = ws2801.hex_to_rgb(data) + ws2801.set_color(r,g,b) + else: + print("incorrect format. use hex color code.") conn.close() # close the connection -start()