用户端的文本文件转换失败

时间:2017-03-15 06:43:08

标签: python mqtt publish-subscribe mosquitto

我正在尝试发布文本文件。格式为.txt。

文本文件如下

273    !Temperature
33     !Pressure
43     !Wind pressure

发布方的代码如下

#!/usr/bin/python

import mosquitto

client = mosquitto.Mosquitto("txt-send")
client.connect("127.0.0.1")

f = open("data.txt")
imagestring = f.read()
byteArray = bytes(imagestring)
client.publish("text", byteArray ,0)

订户方的代码如下

#!/usr/bin/python

import time
import mosquitto

def on_message(mosq, obj, msg):
  with open('received.txt', 'wb') as fd:
    fd.write(msg.payload)
  #I have also tried the following
  #txt = (msg.payload.decode('utf-8'))
  #fd.write(txt)



client = mosquitto.Mosquitto("text-rec")
client.connect("127.0.0.1")
client.subscribe("text",0)
client.on_message = on_message

while True:
   client.loop(15)
   time.sleep(2)

我在文件中获得以下输出

"273/t/t!Temperature/n33/t/t!Pressure/n43/t/t!Wind pressure"

有人可以建议我哪里出错。

0 个答案:

没有答案