如何为PUBACK和PUBREL生成数据包,最重要的是,您如何知道收到的消息的packetId是什么?
我希望能够自己处理PUBREL和PUBACK的逻辑。下面的代码应该暗示我正在尝试做什么。
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
from multiprocessing import Process, Queue
# The callback for when the client receives a CONNACK response from
the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/test")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
def my_function(id):
client = mqtt.Client(client_id="id", clean_session=False,userdata=None, protocol=MQTTv311)
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
msgs = [{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False)]
publish.multiple(msgs, hostname="127.0.0.1", port=1883,client_id=id, keepalive=60,will=None, auth=None, tls=None, protocol=mqtt.MQTTv311)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
for i in range(0,10):
p = Process(target=my_function, args=(str(i))
答案 0 :(得分:0)
在研究可能的MQTT漏洞时遇到了这个问题。据我所知,Paho没有任何模块可以处理更多的低级数据包,因此您可以使用Scapy's MQTT module。
Scapy是Python中的一个模块,允许您处理各种协议的数据包,以下是使用scapy发送MQTT CONNECT的方法:
from scapy.contrib.mqtt import MQTTConnect
from scapy.all import send, IP, TCP
mqtt_broker_ip = '192.168.0.1'
mqtt_broker_port = 1883
ip_pkt = IP(dst=mqtt_broker_ip)
tcp_pkt = TCP(dport=mqtt_broker_port)
mqtt_pkt = MQTTConnect(clientId='my_client_id')
# use slashes (/) to stack protocols
final_pkt = ip_pkt/tcp_pkt/mqtt_pkt
send(mqtt_pkt)