fixed server bug
This commit is contained in:
parent
f71718c5aa
commit
bf797da205
@ -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)
|
||||
while True:
|
||||
conn, address = server_socket.accept() # accept new connection
|
||||
print("Connection from: " + str(address))
|
||||
while True:
|
||||
# receive data stream. it won't accept data packet greater than 1024 bytes
|
||||
data = conn.recv(1024).decode()
|
||||
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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user