I am trying to read the UDP broadcast response from the code below. Two servers are found and the information is returned for both of them. What I want to do is make a dict for each response but I can't seem to separate the individual responses. Can any one make some suggestions please?
import socket
import json
socket.setdefaulttimeout(.5)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
s.sendto("D", ('255.255.255.255', 30303))
while True:
response = s.recv(2048)
response = json.loads(response)
print response
This is the response:
{u'Product': u'OWServer_v2-Enet', u'Name': u'OWServer_v2-Enet_19', u'IP': u'10.0.1.19', u'TCPIntfPort': u'0', u'HTTPPort': u'80', u'MAC': u'00-04-A3-B1-F1-86', u'Bootloader': u'POST', u'FWVer': u'1.44', u'NETBios': u'EDSOWSERVER19 '}
{u'Product': u'OWServer_v2-Enet', u'Name': u'OWServer_v2-Enet_20', u'IP': u'10.0.1.20', u'TCPIntfPort': u'0', u'HTTPPort': u'80', u'MAC': u'00-04-A3-C1-43-86', u'Bootloader': u'POST', u'FWVer': u'1.44', u'NETBios': u'EDSOWSERVER20 '}
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
socket.timeout: timed out
Using your suggestions, if I have done it correctly, gives me ValueErrors.
buf = ""
while True:
buf = buf + s.recv(2048)
resp = ''
if "\n" in buf:
resp, buf = buf.split("\n", 1)
if resp:
print json.loads(resp)
raise ValueError(errmsg("Expecting property name", s, end - 1))
ValueError: Expecting property name: line 1 column 32 (char 32)