Merge pull request #879 from gpotter2/py3-contrib-3

Python 3: fix MQTT
This commit is contained in:
Pierre Lalet 2017-10-24 16:20:31 +02:00 committed by GitHub
commit fe7d8d32ba
2 changed files with 8 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from scapy.fields import FieldLenField, BitEnumField, StrLenField, \
ShortField, ConditionalField, ByteEnumField, ByteField, StrNullField
from scapy.layers.inet import TCP
from scapy.error import Scapy_Exception
from scapy.compat import orb, chb
# CUSTOM FIELDS
@ -25,7 +26,7 @@ class VariableFieldLenField(FieldLenField):
else:
data.append(val)
lastoffset = len(data) - 1
data = "".join(chr(val | (0 if i == lastoffset else 128))
data = b"".join(chb(val | (0 if i == lastoffset else 128))
for i, val in enumerate(data))
return s + data
if len(data) > 3:
@ -35,7 +36,7 @@ class VariableFieldLenField(FieldLenField):
def getfield(self, pkt, s):
value = 0
for offset, curbyte in enumerate(s):
curbyte = ord(curbyte)
curbyte = orb(curbyte)
value += (curbyte & 127) * (128 ** offset)
if curbyte & 128 == 0:
return s[offset + 1:], value

View File

@ -28,8 +28,8 @@ assert(publish.DUP == 0)
assert(publish.RETAIN == 0)
assert(publish.len == 10)
assert(publish[MQTTPublish].length == 4)
assert(publish[MQTTPublish].topic == 'test')
assert(publish[MQTTPublish].value == 'test')
assert(publish[MQTTPublish].topic == b'test')
assert(publish[MQTTPublish].value == b'test')
= MQTTConnect, packet instanciation
@ -42,7 +42,7 @@ assert(c.clientIdlen == 5)
s = b'\x10\x1f\x00\x06MQIsdp\x03\x02\x00<\x00\x11mosqpub/1440-kali'
connect = MQTT(s)
assert(connect.length == 6)
assert(connect.protoname == 'MQIsdp')
assert(connect.protoname == b'MQIsdp')
assert(connect.protolevel == 3)
assert(connect.usernameflag == 0)
assert(connect.passwordflag == 0)
@ -53,7 +53,7 @@ assert(connect.cleansess == 1)
assert(connect.reserved == 0)
assert(connect.klive == 60)
assert(connect.clientIdlen == 17)
assert(connect.clientId == 'mosqpub/1440-kali')
assert(connect.clientId == b'mosqpub/1440-kali')
=MQTTConnack, packet instanciation
@ -82,7 +82,7 @@ s = b'\x82\t\x00\x01\x00\x04test\x00'
subscribe = MQTT(s)
assert(subscribe.msgid == 1)
assert(subscribe.length == 4)
assert(subscribe.topic == 'test')
assert(subscribe.topic == b'test')
assert(subscribe.QOS == 1)