python socket.receive缓冲区

时间:2016-09-25 21:18:43

标签: python sockets

使用脚本轮询智能插件并解析json响应以供进一步使用。

这非常有效:

while True:
  try:
    ip = "192.168.1.130" 
    port = 9999
    sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_tcp.connect((ip, port))
    cmd_emeter = commands['emeter']
    sock_tcp.send(encrypt(cmd_emeter))
    data_emeter = sock_tcp.recv(2048)
    sock_tcp.close()

    # decrypt, parse, and relay emeter response

    emeter = decrypt(data_emeter[4:])
    parsed_json_emeter = json.loads(emeter)
    voltage = parsed_json_emeter['emeter']['get_realtime']['voltage']
    power = parsed_json_emeter['emeter']['get_realtime']['power']
    print voltage
    print power
    publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
    publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")



    time.sleep(5)
  except socket.error:
        quit("Cound not connect to host " + ip + ":" + str(port))

这不是:

while True:
  try:
    ip = "192.168.1.130" 
    port = 9999
    sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_tcp.connect((ip, port))
    cmd_emeter = commands['emeter']
    sock_tcp.send(encrypt(cmd_emeter))
    data_emeter = sock_tcp.recv(2048)
    cmd_info = commands['info']
    sock_tcp.send(encrypt(cmd_info))
    data_info = sock_tcp.recv(2048)
    #cmd_daystats = commands['daystats']
    #sock_tcp.send(encrypt(cmd_daystats))
    #data_daystats = sock_tcp.recv(2048)
    sock_tcp.close()

    # decrypt, parse, and relay emeter response

    emeter = decrypt(data_emeter[4:])
    parsed_json_emeter = json.loads(emeter)
    voltage = parsed_json_emeter['emeter']['get_realtime']['voltage']
    power = parsed_json_emeter['emeter']['get_realtime']['power']
    print voltage
    print power
    publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
    publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")

    # decrypt, parse, and relay info response

    info = decrypt(data_info[4:])
    parsed_json_info = json.loads(info)
    relay_state = parsed_json_info['system']['get_sysinfo']['relay_state']
    print relay_state
    #publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
    #publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")

    time.sleep(5)
  except socket.error:
        quit("Cound not connect to host " + ip + ":" + str(port))

我需要帮助找出原因。也许某种缓冲区问题与sock_tcp.recv(2048)行有关。

以下是错误消息:

pi@raspberrypi:/usr/share/openhab/scripts $ python HS110-relay.py
119.622975
0
Traceback (most recent call last):
File "HS110-relay.py", line 89, in <module>
parsed_json_info = json.loads(info)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

感谢。

猴面包树

0 个答案:

没有答案