#!/bin/python3 import socket import sys import time import math # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Connect the socket to the port where the server is listening server_address = ('0.0.0.0', 2020) print('binding {} port {}'.format(*server_address)) sock.bind(server_address) N_ROWS = 5 N_COLS = 10 oldTime = time.time() oldPing = time.time() def getBytes(): pixels = [0] * (N_ROWS * N_COLS) for row in range(N_ROWS): for col in range(N_COLS): pixels[row*N_COLS + col] = int(math.sin(row + col + time.time()*15) * 8 + 8) pixels[49] = int(pixels[49]/2) return bytes([0x41] + pixels) client_address = None try: while True: if not client_address: data, addr = sock.recvfrom(16) print('received {} from {}'.format(data, addr)) client_address = addr else: sock.sendto(getBytes(), client_address) if time.time() - oldPing > 30: data, addr = sock.recvfrom(16) print('received {} from {}'.format(data, addr)) client_address = addr oldPing = time.time() oldTime = time.time() time.sleep(1/40) finally: print('closing socket') sock.close()