Last active
September 25, 2024 08:06
-
-
Save tiagocoutinho/88c99bb277cd9fb2a5b2fd1b4315d2fc to your computer and use it in GitHub Desktop.
read temp periodically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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