我通过应用发明家发送带有http的消息来完成 通过发送图片和下面的代码之类的网址
我想知道MQTT是否可以接收相同类型的url或JSON 连接客户端并发送消息
from http.server import BaseHTTPRequestHandler, HTTPServer
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
global Request, test, data1, data2, state, case
messagetosend = bytes('test', "utf")
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
Request = self.requestline
Request = Request[5: int(len(Request)-9)]
return
server_address_httpd = ('192.168.66.19', 8080)
httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
httpd.serve_forever()
这是我尝试使用的MQTT客户端的python代码
mqtt.Client(transport="websockets")
但我不确定是否可行
import paho.mqtt.client as mqtt
from test1 import abc
import datetime
def on_connect(clien, userdata, flags, rc):
print("connected with" + str(rc))
client.subscribe("epaper/headphoto")
def on_message(client, userdata, msg):
print(msg.topic + "" + str(msg.payload))
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("loclhost", 1883, 60)
client.loop_forever()