import base64
import paho.mqtt.client as mqtt
import time
import bs4
ip = '192.168.17.128'
port = 1883
messagejson = {'a': 'asdshd', 'b': 2334}
def on_connect(clinet, userdata, flag, rc):
print(rc)
# 发送json文字
def sendjsonmessage():
client.publish('hello', str(messagejson))
# 发送json文件
def sendjsonfilemessage():
with open('train.jsonl', 'r', encoding='utf8') as jsonfp:
a = jsonfp.readlines()
for line in a:
client.publish('hello', str(line))
print(str(line))
# 发送图片信息
def sendjpgfile():
# with open('')
with open('1.jpg', 'rb') as jpgfile:
client.publish('hello', base64.b64encode(jpgfile.read()).decode('utf-8'))
# 发送语音
def sendvoice():
with open('received_voice.wav', 'rb') as voicefp:
file = voicefp.read()
client.publish('hello', base64.b64encode(file).decode('utf-8'))
client = mqtt.Client()
client.on_connect = on_connect
client.connect(ip, port, 60)
client.loop_start()
try:
while True:
time.sleep(1)
# 发送json文字
# sendjsonmessage()
# sendjsonfilemessage()
# 发送声音信息
# sendvoice()
# 发送图片信息
sendjpgfile()
break
except KeyboardInterrupt:
client.loop_stop()
client.disconnect()
python练习1
最新推荐文章于 2025-06-08 02:52:26 发布