34 lines
996 B
Python
34 lines
996 B
Python
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"
|
|
port = 5000
|
|
|
|
server_socket = socket.socket()
|
|
server_socket.bind((host, port))
|
|
|
|
# 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))
|
|
# receive data stream. it won't accept data packet greater than 1024 bytes
|
|
while True:
|
|
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
|
|
|