fixed server bug

This commit is contained in:
Lucca Ketterer 2020-06-08 00:03:11 +02:00
parent f71718c5aa
commit bf797da205

View File

@ -1,5 +1,10 @@
import socket import socket
import ws2801 import ws2801
import re
def validate_data(data):
reg = re.compile("[0-9a-f]{6}$")
return reg.match(data)
def start(): def start():
host = "0.0.0.0" host = "0.0.0.0"
@ -10,14 +15,18 @@ def start():
# configure how many client the server can listen simultaneously # configure how many client the server can listen simultaneously
server_socket.listen(1) server_socket.listen(1)
conn, address = server_socket.accept() # accept new connection
print("Connection from: " + str(address))
while True: 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 # receive data stream. it won't accept data packet greater than 1024 bytes
data = conn.recv(1024).decode() data = conn.recv(1024).decode()
r,g,b = ws2801.hex_to_rgb(data) if not data:
ws2801.set_color(r,g,b) 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 conn.close() # close the connection
start()