This commit is contained in:
Théophyl Hoguet committed 2021-12-17 16:49:01 +01:00
1 parent 75e37f8ac7
commit 84cb2d6643
3 files changed
+80 -60

No files matched your search

Binary file not shown.
+76 -60
View File
@@ -16,105 +16,114 @@ TYPE_PUBLISH = 0x30
TYPE_SUBREQ = 0x82
TYPE_SUBACK = 0x90
def create_mqtt_publish_msg(topic, value, retain=False):
""" create mqtt publish msg
>>> create_mqtt_publish_msg("temp","45")
b'0\\x08\\x00\\x04temp45'
"""
>>> create_mqtt_publish_msg("temp","45")
b'0\\x08\\x00\\x04temp45'
"""
retain_code = 0
if retain:
retain_code = 1
#on cherche la taille de topic et value
# on cherche la taille de topic et value
topic.encode("ascii")
topic_length = len(topic)
value.encode("ascii")
value_length = len(value)
#total
# total
message_length = value_length + topic_length + 2
request = (TYPE_PUBLISH + retain_code).to_bytes(1, byteorder="big") + (message_length).to_bytes(1, byteorder = 'big') + (topic_length).to_bytes(2, byteorder = 'big') + topic.encode("ascii") + value.encode("ascii")
request = (TYPE_PUBLISH + retain_code).to_bytes(1, byteorder="big") + (message_length).to_bytes(1, byteorder='big') + (topic_length).to_bytes(
2, byteorder='big') + topic.encode("ascii") + value.encode("ascii")
return request
def create_mqtt_connect_msg(client_ID):
client_ID = client_ID.encode("ascii")
client_ID_length = len(client_ID).to_bytes(2,byteorder="big")
header = (TYPE_CONNECT).to_bytes(1, byteorder = "big")
def create_mqtt_connect_msg(client_ID):
client_ID = client_ID.encode("ascii")
client_ID_length = len(client_ID).to_bytes(2, byteorder="big")
header = (TYPE_CONNECT).to_bytes(1, byteorder="big")
proto_name = "MQTT".encode("ascii")
proto_lenght = len(proto_name).to_bytes(2, byteorder="big")
version = (4).to_bytes(1,byteorder="big")
connect_flags = (0x02).to_bytes(1,byteorder="big")
version = (4).to_bytes(1, byteorder="big")
connect_flags = (0x02).to_bytes(1, byteorder="big")
keepalive = (60).to_bytes(2, byteorder="big")
message_length = (2 + 4 + 1 + 1 + 2 + int.from_bytes(client_ID_length,byteorder="big") + 2).to_bytes(1,byteorder="big") # size protocol name length + size protocol name + size version + size CONNECT flags + size keep alive + size client id length + size client ID
return (header + message_length + proto_lenght + proto_name + version + connect_flags + keepalive + client_ID_length + client_ID)
# size protocol name length + size protocol name + size version + size CONNECT flags + size keep alive + size client id length + size client ID
message_length = (2 + 4 + 1 + 1 + 2 + int.from_bytes(client_ID_length, byteorder="big") + 2).to_bytes(1, byteorder="big")
return (header + message_length + proto_lenght + proto_name + version + connect_flags + keepalive + client_ID_length + client_ID)
def create_mqtt_subscriber_msg(topic):
topic = topic.encode("ascii")
topic_length = len(topic).to_bytes(2, byteorder = "big")
message_identifier = (1).to_bytes(2, byteorder = "big")
header = (TYPE_SUBREQ).to_bytes(1, byteorder = "big")
requested_qos = (0).to_bytes(1, byteorder = "big")
message_length = (1 + 2 + 2 + 6 + 1).to_bytes(1, byteorder = "big")
topic_length = len(topic).to_bytes(2, byteorder="big")
message_identifier = (1).to_bytes(2, byteorder="big")
header = (TYPE_SUBREQ).to_bytes(1, byteorder="big")
requested_qos = (0).to_bytes(1, byteorder="big")
message_length = (1 + 2 + 2 + 6 + 1).to_bytes(1, byteorder="big")
return (header + message_length + message_identifier + topic_length + topic + requested_qos)
def create_mqtt_suback_msg():
header = (TYPE_SUBACK).to_bytes(1, byteorder = "big")
message_identifier = (1).to_bytes(2, byteorder = "big")
requested_qos = (0).to_bytes(1, byteorder = "big")
message_length = (2 + 1).to_bytes(1, byteorder = "big")
header = (TYPE_SUBACK).to_bytes(1, byteorder="big")
message_identifier = (1).to_bytes(2, byteorder="big")
requested_qos = (0).to_bytes(1, byteorder="big")
message_length = (2 + 1).to_bytes(1, byteorder="big")
return (header + message_length + message_identifier + requested_qos)
def create_mqtt_connack_msg(accepted=True):
header = (TYPE_CONNACK).to_bytes(1,byteorder="big")
msglen = (2).to_bytes(1,byteorder="big")
flags = (0).to_bytes(1,byteorder="big")
header = (TYPE_CONNACK).to_bytes(1, byteorder="big")
msglen = (2).to_bytes(1, byteorder="big")
flags = (0).to_bytes(1, byteorder="big")
if accepted:
returncode = (0).to_bytes(1,byteorder="big")
returncode = (0).to_bytes(1, byteorder="big")
else:
returncode = (1).to_bytes(1,byteorder="big")
returncode = (1).to_bytes(1, byteorder="big")
return header + msglen + flags + returncode
def decode_msg(msg):
if msg[:1] == TYPE_CONNECT.to_bytes(1,byteorder="big"):
typemsg = "CONNECT"
keepalive = int.from_bytes(msg[11:12],byteorder="big")
clientid = msg[14:].decode("ascii")
return (typemsg,keepalive,clientid)
if msg[:1] == TYPE_CONNACK.to_bytes(1,byteorder='big'):
def decode_msg(msg):
if msg[:1] == TYPE_CONNECT.to_bytes(1, byteorder="big"):
typemsg = "CONNECT"
keepalive = int.from_bytes(msg[11:12], byteorder="big")
clientid = msg[14:].decode("ascii")
return (typemsg, keepalive, clientid)
if msg[:1] == TYPE_CONNACK.to_bytes(1, byteorder='big'):
typemsg = "CONNACK"
ackflags = msg[2:3].decode('ascii')
code = int.from_bytes(msg[3:4],byteorder="big")
return (typemsg,ackflags,code)
code = int.from_bytes(msg[3:4], byteorder="big")
return (typemsg, ackflags, code)
if msg[:1] == TYPE_SUBREQ.to_bytes(1,byteorder="big"):
if msg[:1] == TYPE_SUBREQ.to_bytes(1, byteorder="big"):
typemsg = "SUBREQ"
msgid = int.from_bytes(msg[2:4],byteorder="big")
topiclenght = int.from_bytes(msg[4:6],byteorder="big")
topic = msg[6:6+topiclenght].decode('ascii')
return (typemsg,msgid,topic)
msgid = int.from_bytes(msg[2:4], byteorder="big")
topiclenght = int.from_bytes(msg[4:6], byteorder="big")
topic = msg[6:6 + topiclenght].decode('ascii')
return (typemsg, msgid, topic)
if msg[:1] == TYPE_SUBACK.to_bytes(1,byteorder="big"):
if msg[:1] == TYPE_SUBACK.to_bytes(1, byteorder="big"):
typemsg = "SUBACK"
msgid = int.from_bytes(msg[2:4],byteorder="big")
return (typemsg,msgid)
msgid = int.from_bytes(msg[2:4], byteorder="big")
return (typemsg, msgid)
if msg[:1] == TYPE_PUBLISH.to_bytes(1,byteorder="big") or msg[:1] == (TYPE_PUBLISH+1).to_bytes(1,byteorder="big"):
if msg[:1] == TYPE_PUBLISH.to_bytes(1, byteorder="big") or msg[:1] == (TYPE_PUBLISH + 1).to_bytes(1, byteorder="big"):
typemsg = "PUBLISH"
topiclenght = int.from_bytes(msg[2:4],byteorder="big")
topic = msg[4:4+topiclenght].decode('ascii')
message = msg[4+topiclenght:].decode('ascii')
if msg[:1] == TYPE_PUBLISH.to_bytes(1,byteorder="big"):
return (typemsg,topic,message,False)
return (typemsg,topic,message,True)
topiclenght = int.from_bytes(msg[2:4], byteorder="big")
topic = msg[4:4 + topiclenght].decode('ascii')
message = msg[4 + topiclenght:].decode('ascii')
if msg[:1] == TYPE_PUBLISH.to_bytes(1, byteorder="big"):
return (typemsg, topic, message, False)
return (typemsg, topic, message, True)
return tuple("ERROR")
# print(create_mqtt_connect_msg("mosq-6F4yNCdkrVx80t8BVp"))
# print(decode_msg(create_mqtt_connect_msg("mosq-6F4yNCdkrVx80t8BVp")))
# print(create_mqtt_publish_msg("monsieur ", "théophyl"))
# print(decode_msg(create_mqtt_publish_msg("monsieur ", "théophyl")))
def run_publisher(addr, topic, pub_id, retain=False):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(addr)
@@ -123,7 +132,8 @@ def run_publisher(addr, topic, pub_id, retain=False):
if decode_msg(connack)[0] == "CONNACK" and decode_msg(connack)[2] == 0:
while True:
msg = sys.stdin.readline()
s.sendall(create_mqtt_publish_msg(topic,msg,retain))
sys.stdin.flush()
s.sendall(create_mqtt_publish_msg(topic, msg, retain))
pass
@@ -139,7 +149,7 @@ def run_subscriber(addr, topic, sub_id):
while True:
tmp = decode_msg(s.recv(127))
if tmp[0] == "PUBLISH":
print(tmp[1] + ' : ' + tmp[2])
print(tmp[1] + ' : ' + tmp[2], end="")
pass
@@ -150,8 +160,9 @@ def run_server(addr):
s.listen(1)
l = [s]
listconnected = []
retainmsg = [False, "", ""]
while True:
l3, _, _ = select.select(l,[],[])
l3, _, _ = select.select(l, [], [])
for i in l3:
if i == s:
conn, _ = i.accept()
@@ -161,14 +172,19 @@ def run_server(addr):
data = decode_msg(data)
if data[0] == "CONNECT":
i.sendall(create_mqtt_connack_msg(True))
listconnected.append([i,None])
listconnected.append([i, None])
elif data[0] == "SUBREQ":
for o in listconnected:
if i in o:
o[1] = data[2]
i.sendall(create_mqtt_suback_msg())
i.sendall(create_mqtt_suback_msg())
if retainmsg[0]:
if retainmsg[1] == data[2]:
i.sendall(create_mqtt_publish_msg(data[2], retainmsg[2]))
elif data[0] == "PUBLISH":
if data[3]:
retainmsg = (True, data[1], data[2])
for o in listconnected:
if data[1] in o:
o[0].sendall(create_mqtt_publish_msg(data[1],data[2],data[3]))
o[0].sendall(create_mqtt_publish_msg(data[1], data[2], data[3]))
pass
+4
View File
@@ -0,0 +1,4 @@
test
123
IJN
MLK