Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Last active September 25, 2024 08:06
Show Gist options
  • Save tiagocoutinho/88c99bb277cd9fb2a5b2fd1b4315d2fc to your computer and use it in GitHub Desktop.
Save tiagocoutinho/88c99bb277cd9fb2a5b2fd1b4315d2fc to your computer and use it in GitHub Desktop.
read temp periodically
import time
import socket
def temp_stream():
with socket.create_connection(("localhost", 10_123)) as sock:
sockfd = sock.makefile("rwb", buffering=1)
while True:
sockfd.write(b"TEMP?\n")
payload = sockfd.readline()
yield float(payload)
for temp in temp_stream():
print(f"{temp=}")
time.sleep(1)
import socket
import random
with socket.create_server(("", 10_123)) as serv:
while True:
client, _ = serv.accept()
clientfd = client.makefile("rwb", buffering=1)
with client:
while True:
req = clientfd.readline()
if not req:
break
assert req == b'TEMP?\n'
clientfd.write(f"{random.random()*20}\n".encode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment