Created
November 3, 2014 17:38
-
-
Save Jonty/8aef635600f3a8601fdb to your computer and use it in GitHub Desktop.
Stub tree updater in python for Charles
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
from threading import Thread, Timer | |
import socket | |
# Open a UDP netcat session to this with `nc -vu localhost 1337` | |
# It takes updates in the form "key value" and will print out the current state from the "sender" thread every 5s | |
state = { | |
} | |
def read_state(socket): | |
while True: | |
data, addr = socket.recvfrom(1024) | |
update = {} | |
try: | |
for line in data.splitlines(): | |
key, value = line.split(" ") | |
update[key] = value | |
state.update(update) | |
except Exception, e: | |
print "Error parsing update: ", e | |
def send_state(): | |
for key, value in state.items(): | |
# This is where you should be firing off UDP packets | |
print key, value | |
# This should be 0.04 for 25fps, is 5 for testing | |
Timer(5, send_state).start() | |
if __name__ == "__main__": | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind(("0.0.0.0", 1337)) | |
send_state() | |
t = Thread(target=read_state, args=(sock,)) | |
t.start() | |
t.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment