urcontrol/tests/socketreceivetest.py

41 lines
984 B
Python
Raw Normal View History

2022-05-31 13:05:06 +00:00
import socket
import math
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host =""
port =30002
s.bind((host,port))
s.listen(1) # Number of connections
s.setblocking(False)
2022-05-31 13:26:53 +00:00
client = None
2022-05-31 13:05:06 +00:00
while True:
try:
client, address = s.accept()
print("Connected to", address)
except socket.error:
pass
2022-05-31 13:26:53 +00:00
if client is not None:
try:
data = client.recv( 1024 ).decode( 'utf-8' )
if data:
2022-05-31 13:34:27 +00:00
lastdata=data.split('\n')
#print("Received :", repr(data))
2022-05-31 14:18:56 +00:00
if (len(lastdata)>1):
splitdata=lastdata[-2].split(",")
try:
pan=float(splitdata[0])
tilt=float(splitdata[1])
print("Pan="+str(pan)+" Tilt="+str(tilt))
except ValueError:
print("Not a float")
2022-05-31 13:26:53 +00:00
except socket.error:
pass
2022-05-31 13:05:06 +00:00
s.close()