本文整理汇总了Python中coapthon.serializer.Serializer.serialize方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.serialize方法的具体用法?Python Serializer.serialize怎么用?Python Serializer.serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类coapthon.serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_separate
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_separate(self, message, notification):
serializer = Serializer()
datagram = serializer.serialize(message)
self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
datagram, source = self.tr.written[0]
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, defines.inv_types["ACK"])
self.assertEqual(message.code, None)
self.assertEqual(message.mid, self.current_mid + 4)
self.assertEqual(message.source, source)
datagram, source = self.tr.written[1]
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, notification.type)
self.assertEqual(message.code, notification.code)
self.assertEqual(message.source, source)
self.assertEqual(message.token, notification.token)
self.assertEqual(message.payload, notification.payload)
self.assertEqual(message.options, notification.options)
self.tr.written = []
message = Message.new_ack(message)
datagram = serializer.serialize(message)
self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
self.tr.written = []
示例2: _test_modular
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_modular(self, lst):
serializer = Serializer()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for t in lst:
message, expected = t
send_ack = False
if message is not None:
datagram = serializer.serialize(message)
sock.sendto(datagram, self.server_address)
else:
send_ack = True
datagram, source = sock.recvfrom(4096)
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, expected.type)
if not send_ack:
self.assertEqual(message.mid, expected.mid)
self.assertEqual(message.code, expected.code)
self.assertEqual(message.source, source)
self.assertEqual(message.token, expected.token)
self.assertEqual(message.payload, expected.payload)
self.assertEqual(message.options, expected.options)
if send_ack:
message = Message.new_ack(message)
datagram = serializer.serialize(message)
sock.sendto(datagram, self.server_address)
sock.close()
示例3: _test_modular
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_modular(self, lst):
serializer = Serializer()
for t in lst:
message, expected = t
send_ack = False
if message is not None:
datagram = serializer.serialize(message)
self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
else:
send_ack = True
datagram, source = self.tr.written.pop(0)
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, expected.type)
if not send_ack:
self.assertEqual(message.mid, expected.mid)
self.assertEqual(message.code, expected.code)
self.assertEqual(message.source, source)
self.assertEqual(message.token, expected.token)
self.assertEqual(message.payload, expected.payload)
self.assertEqual(message.options, expected.options)
if send_ack:
message = Message.new_ack(message)
datagram = serializer.serialize(message)
self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
self.tr.written = []
示例4: _test_separate
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_separate(self, message, notification):
serializer = Serializer()
datagram = serializer.serialize(message)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(datagram, self.server_address)
datagram, source = sock.recvfrom(4096)
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, defines.inv_types["ACK"])
self.assertEqual(message.code, None)
self.assertEqual(message.mid, self.current_mid - 1)
self.assertEqual(message.source, source)
datagram, source = sock.recvfrom(4096)
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, notification.type)
self.assertEqual(message.code, notification.code)
self.assertEqual(message.source, source)
self.assertEqual(message.token, notification.token)
self.assertEqual(message.payload, notification.payload)
self.assertEqual(message.options, notification.options)
message = Message.new_ack(message)
datagram = serializer.serialize(message)
sock.sendto(datagram, self.server_address)
sock.close()
示例5: send_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send_datagram(self, message):
host, port = message.destination
logger.debug("send_datagram - " + str(message))
serializer = Serializer()
message = serializer.serialize(message)
self._socket.sendto(message, (host, port))
示例6: _test_plugtest
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_plugtest(self, message, expected):
serializer = Serializer()
datagram = serializer.serialize(message)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(datagram, message.destination)
datagram, source = sock.recvfrom(4096)
host, port = source
message = serializer.deserialize(datagram, host, port)
if expected.type is not None:
self.assertEqual(message.type, expected.type)
if expected.mid is not None:
self.assertEqual(message.mid, expected.mid)
self.assertEqual(message.code, expected.code)
if expected.source is not None:
self.assertEqual(message.source, source)
if expected.token is not None:
self.assertEqual(message.token, expected.token)
if expected.payload is not None:
self.assertEqual(message.payload, expected.payload)
if expected.options is not None:
self.assertEqual(message.options, expected.options)
sock.close()
示例7: send
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send(self, request, endpoint, resend=False):
self._endpoint = endpoint
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._receiver_thread = threading.Thread(target=self.datagram_received)
self._receiver_thread.start()
if not resend:
if request.mid is None:
request.mid = self._currentMID
self._currentMID += 1
key = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(request.mid))
key_token = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(request.token))
self.sent[key] = (request, time.time())
self.sent[key_token] = request
if request.type is None:
request.type = defines.inv_types["CON"]
serializer = Serializer()
request.destination = self._endpoint
host, port = request.destination
print "Message sent to " + host + ":" + str(port)
print "----------------------------------------"
print request
print "----------------------------------------"
datagram = serializer.serialize(request)
log.msg("Send datagram")
self._socket.sendto(datagram, self._endpoint)
示例8: _test_plugtest
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test_plugtest(self, message_list): # pragma: no cover
serializer = Serializer()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for message, expected in message_list:
if message is not None:
datagram = serializer.serialize(message)
sock.sendto(datagram, message.destination)
if expected is not None:
datagram, source = sock.recvfrom(4096)
received_message = serializer.deserialize(datagram, source)
if expected.type is not None:
self.assertEqual(received_message.type, expected.type)
if expected.mid is not None:
self.assertEqual(received_message.mid, expected.mid)
self.assertEqual(received_message.code, expected.code)
if expected.source is not None:
self.assertEqual(received_message.source, source)
if expected.token is not None:
self.assertEqual(received_message.token, expected.token)
if expected.payload is not None:
self.assertEqual(received_message.payload, expected.payload)
if expected.options is not None:
self.assertEqual(received_message.options, expected.options)
for o in expected.options:
option_value = getattr(expected, o.name.lower().replace("-", "_"))
option_value_rec = getattr(received_message, o.name.lower().replace("-", "_"))
self.assertEqual(option_value, option_value_rec)
sock.close()
示例9: coap_encode
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def coap_encode(payload):
message = Message()
message.type = defines.Types['CON']
message.token = 4321
message.mid = 2
message.options = None
message.payload = str(payload)
serializer = Serializer()
messagestring = serializer.serialize(message)
return messagestring
示例10: send
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send(self, message):
serializer = Serializer()
message.destination = self.server
host, port = message.destination
print "Message sent to " + host + ":" + str(port)
print "----------------------------------------"
print message
print "----------------------------------------"
datagram = serializer.serialize(message)
log.msg("Send datagram")
self.transport.write(datagram, self.server)
示例11: test_etag_deserialize
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def test_etag_deserialize(self):
req = Request()
req.type = defines.Types["CON"]
req._mid = 1
req.etag = bytearray([0xC5])
serializer = Serializer()
serialized = serializer.serialize(req)
received_message = serializer.deserialize(serialized, ("127.0.0.1", 5683))
self.assertEqual(req.etag, received_message.etag)
示例12: send_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send_datagram(self, message):
"""
:type message: Message
:param message:
"""
if not self.stopped.isSet():
host, port = message.destination
logger.debug("send_datagram - " + str(message))
serializer = Serializer()
message = serializer.serialize(message)
self._socket.sendto(message, (host, port))
示例13: send
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send(self, message, host, port):
"""
Send the message
:param message: the message to send
:param host: destination host
:param port: destination port
"""
# print "Message send to " + host + ":" + str(port)
# print "----------------------------------------"
# print message
# print "----------------------------------------"
serializer = Serializer()
message = serializer.serialize(message)
self._socket.sendto(message, (host, port))
示例14: _test
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def _test(self, message, expected):
serializer = Serializer()
datagram = serializer.serialize(message)
self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
datagram, source = self.tr.written[-1]
host, port = source
message = serializer.deserialize(datagram, host, port)
self.assertEqual(message.type, expected.type)
self.assertEqual(message.mid, expected.mid)
self.assertEqual(message.code, expected.code)
self.assertEqual(message.source, source)
self.assertEqual(message.token, expected.token)
self.assertEqual(message.payload, expected.payload)
self.assertEqual(message.options, expected.options)
self.tr.written = []
示例15: send_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import serialize [as 别名]
def send_datagram(self, message):
"""
Send a message through the udp socket.
:type message: Message
:param message: the message to send
"""
if not self.stopped.isSet():
host, port = message.destination
logger.debug("send_datagram - " + str(message))
serializer = Serializer()
message = serializer.serialize(message)
if self.multicast:
self._unicast_socket.sendto(message, (host, port))
else:
self._socket.sendto(message, (host, port))