lighting-controller/networking.py

21 lines
772 B
Python

import socket
import asyncio
async def send_color_data(color_data, servers):
"""Send color data to all configured servers."""
tasks = [send_to_server(color_data, server) for server in servers]
await asyncio.gather(*tasks)
async def send_to_server(color_data, server):
"""Connect to a server, send color data, and close the connection."""
server_ip, server_port = server
try:
reader, writer = await asyncio.open_connection(server_ip, server_port)
writer.write(color_data)
await writer.drain()
writer.close()
await writer.wait_closed()
print(f"Sent data to {server_ip}:{server_port}")
except (socket.error, ConnectionError) as e:
print(f"Error sending to {server_ip}:{server_port}: {e}")