本文整理汇总了Python中coapthon.serializer.Serializer.deserialize方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.deserialize方法的具体用法?Python Serializer.deserialize怎么用?Python Serializer.deserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类coapthon.serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.deserialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_separate
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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_separate
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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()
示例3: _test_plugtest
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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()
示例4: receive_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def receive_datagram(self):
"""
Receive datagram from the UDP socket and invoke the callback function.
"""
logger.debug("Start receiver Thread")
while not self.stopped.isSet():
self._socket.settimeout(0.1)
try:
datagram, addr = self._socket.recvfrom(1152)
except socket.timeout: # pragma: no cover
continue
except Exception as e: # pragma: no cover
if self._cb_ignore_read_exception is not None and callable(self._cb_ignore_read_exception):
if self._cb_ignore_read_exception(e, self):
continue
return
else: # pragma: no cover
if len(datagram) == 0:
logger.debug("Exiting receiver Thread due to orderly shutdown on server end")
return
serializer = Serializer()
try:
host, port = addr
except ValueError:
host, port, tmp1, tmp2 = addr
source = (host, port)
message = serializer.deserialize(datagram, source)
if isinstance(message, Response):
logger.debug("receive_datagram - " + str(message))
transaction, send_ack = self._messageLayer.receive_response(message)
if transaction is None: # pragma: no cover
continue
self._wait_for_retransmit_thread(transaction)
if send_ack:
self._send_ack(transaction)
self._blockLayer.receive_response(transaction)
if transaction.block_transfer:
self._send_block_request(transaction)
continue
elif transaction is None: # pragma: no cover
self._send_rst(transaction)
return
self._observeLayer.receive_response(transaction)
if transaction.notification: # pragma: no cover
ack = Message()
ack.type = defines.Types['ACK']
ack = self._messageLayer.send_empty(transaction, transaction.response, ack)
self.send_datagram(ack)
self._callback(transaction.response)
else:
self._callback(transaction.response)
elif isinstance(message, Message):
self._messageLayer.receive_empty(message)
logger.debug("Exiting receiver Thread due to request")
示例5: _test_modular
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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()
示例6: _test_plugtest
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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: _test_modular
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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 = []
示例8: datagramReceived
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def datagramReceived(self, datagram, host):
serializer = Serializer()
host, port = host
message = serializer.deserialize(datagram, host, port)
print "Message received from " + host + ":" + str(port)
print "----------------------------------------"
print message
print "----------------------------------------"
if isinstance(message, Response):
self.handle_response(message)
elif isinstance(message, Request):
log.err("Received request")
else:
self.handle_message(message)
key = hash(str(host) + str(port) + str(message.mid))
if message.type == defines.inv_types["ACK"] and message.code == defines.inv_codes["EMPTY"] \
and key in self.sent.keys():
# Separate Response
print "Separate Response"
else:
function, args, kwargs, client_callback = self.get_operation()
key = hash(str(host) + str(port) + str(message.token))
if function is None and len(self.relation) == 0:
if not self._forward:
reactor.stop()
elif key in self.relation:
response, timestamp, client_callback = self.relation.get(key)
self.handle_notification(message, client_callback)
else:
function(client_callback, *args, **kwargs)
示例9: listen
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def listen(self, timeout=10):
"""
Listen for incoming messages. Timeout is used to check if the server must be switched off.
:param timeout: Socket Timeout in seconds
"""
self._socket.settimeout(float(timeout))
while not self.stopped.isSet():
try:
data, client_address = self._socket.recvfrom(4096)
if len(client_address) > 2:
client_address = (client_address[0], client_address[1])
except socket.timeout:
continue
try:
serializer = Serializer()
message = serializer.deserialize(data, client_address)
if isinstance(message, int):
logger.error("receive_datagram - BAD REQUEST")
rst = Message()
rst.destination = client_address
rst.type = defines.Types["RST"]
rst.code = message
rst.mid = self._messageLayer._current_mid
self._messageLayer._current_mid += 1 % 65535
self.send_datagram(rst)
continue
logger.debug("receive_datagram - " + str(message))
if isinstance(message, Request):
transaction = self._messageLayer.receive_request(message)
if transaction.request.duplicated and transaction.completed:
logger.debug("message duplicated, transaction completed")
if transaction.response is not None:
self.send_datagram(transaction.response)
return
elif transaction.request.duplicated and not transaction.completed:
logger.debug("message duplicated, transaction NOT completed")
self._send_ack(transaction)
return
args = (transaction, )
t = threading.Thread(target=self.receive_request, args=args)
t.start()
# self.receive_datagram(data, client_address)
elif isinstance(message, Response):
logger.error("Received response from %s", message.source)
else: # is Message
transaction = self._messageLayer.receive_empty(message)
if transaction is not None:
with transaction:
self._blockLayer.receive_empty(message, transaction)
self._observeLayer.receive_empty(message, transaction)
except RuntimeError:
print "Exception with Executor"
self._socket.close()
示例10: test_etag_deserialize
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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)
示例11: receive_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def receive_datagram(self, args):
"""
Handle messages coming from the udp socket.
:param args: (data, client_address)
"""
data, client_address = args
logging.debug("receiving datagram")
try:
host, port = client_address
except ValueError:
host, port, tmp1, tmp2 = client_address
client_address = (host, port)
serializer = Serializer()
message = serializer.deserialize(data, client_address)
if isinstance(message, int):
logger.error("receive_datagram - BAD REQUEST")
rst = Message()
rst.destination = client_address
rst.type = Types["RST"]
rst.code = message
rst.mid = message.mid
self.send_datagram(rst)
return
logger.debug("receive_datagram - " + str(message))
if isinstance(message, Request):
if not message.proxy_uri or message.uri_path != "coap2http":
logger.error("receive_datagram - BAD REQUEST")
rst = Message()
rst.destination = client_address
rst.type = Types["RST"]
rst.code = Codes.BAD_REQUEST.number
rst.mid = message.mid
self.send_datagram(rst)
return
# Execute HTTP/HTTPS request
http_response = CoAP_HTTP.execute_http_request(message.code, message.proxy_uri, message.payload)
# HTTP response to CoAP response conversion
coap_response = CoAP_HTTP.to_coap_response(http_response, message.code, client_address, message.mid)
# Send datagram and return
self.send_datagram(coap_response)
return
elif isinstance(message, Message):
logger.error("Received message from %s", message.source)
else: # is Response
logger.error("Received response from %s", message.source)
示例12: receive_datagram
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def receive_datagram(self):
logger.debug("Start receiver Thread")
while not self.stopped.isSet():
self._socket.settimeout(1)
try:
datagram, addr = self._socket.recvfrom(1152)
except socket.timeout: # pragma: no cover
continue
except socket.error: # pragma: no cover
return
else: # pragma: no cover
if len(datagram) == 0:
print 'orderly shutdown on server end'
return
serializer = Serializer()
try:
host, port = addr
except ValueError:
host, port, tmp1, tmp2 = addr
source = (host, port)
message = serializer.deserialize(datagram, source)
if isinstance(message, Response):
transaction, send_ack = self._messageLayer.receive_response(message)
if transaction is None: # pragma: no cover
continue
if send_ack:
self._send_ack(transaction)
self._blockLayer.receive_response(transaction)
if transaction.block_transfer:
transaction = self._messageLayer.send_request(transaction.request)
self.send_datagram(transaction.request)
continue
elif transaction is None: # pragma: no cover
self._send_rst(transaction)
return
self._observeLayer.receive_response(transaction)
if transaction.notification: # pragma: no cover
ack = Message()
ack.type = defines.Types['ACK']
ack = self._messageLayer.send_empty(transaction, transaction.response, ack)
self.send_datagram(ack)
self._callback(transaction.response)
else:
self._callback(transaction.response)
elif isinstance(message, Message):
self._messageLayer.receive_empty(message)
示例13: datagramReceived
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def datagramReceived(self, data, addr):
"""
Handler for received UDP datagram.
:param data: the UDP datagram
:param host: source host
:param port: source port
"""
try:
host, port = addr
except ValueError:
host, port, tmp1, tmp2 = addr
log.msg("Datagram received from " + str(host) + ":" + str(port))
serializer = Serializer()
message = serializer.deserialize(data, host, port)
print "Message received from " + host + ":" + str(port)
print "----------------------------------------"
print message
print "----------------------------------------"
if isinstance(message, Request):
log.msg("Received request")
ret = self.request_layer.handle_request(message)
if isinstance(ret, Request):
response = self.request_layer.process(ret)
else:
response = ret
self.schedule_retrasmission(message, response, None)
log.msg("Send Response")
self.send(response, host, port)
elif isinstance(message, Response):
log.err("Received response")
rst = Message.new_rst(message)
rst = self.message_layer.matcher_response(rst)
log.msg("Send RST")
self.send(rst, host, port)
elif isinstance(message, tuple):
message, error = message
response = Response()
response.destination = (host, port)
response.code = defines.responses[error]
response = self.reliability_response(message, response)
response = self.message_layer.matcher_response(response)
log.msg("Send Error")
self.send(response, host, port)
elif message is not None:
# ACK or RST
log.msg("Received ACK or RST")
self.message_layer.handle_message(message)
示例14: _test
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [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: finish_request
# 需要导入模块: from coapthon.serializer import Serializer [as 别名]
# 或者: from coapthon.serializer.Serializer import deserialize [as 别名]
def finish_request(self, args):
"""
Handler for received UDP datagram.
:param args: (data, (client_ip, client_port)
"""
data, client_address = args
host = client_address[0]
port = client_address[1]
# logging.log(logging.INFO, "Datagram received from " + str(host) + ":" + str(port))
serializer = Serializer()
message = serializer.deserialize(data, host, port)
# print "Message received from " + host + ":" + str(port)
# print "----------------------------------------"
# print message
# print "----------------------------------------"
if isinstance(message, Request):
# log.msg("Received request")
ret = self.request_layer.handle_request(message)
if isinstance(ret, Request):
response = self.request_layer.process(ret)
else:
response = ret
self.schedule_retrasmission(message, response, None)
# log.msg("Send Response")
return response, host, port
elif isinstance(message, Response):
# log.err("Received response")
rst = Message.new_rst(message)
rst = self.message_layer.matcher_response(rst)
# log.msg("Send RST")
return rst, host, port
elif isinstance(message, tuple):
message, error = message
response = Response()
response.destination = (host, port)
response.code = defines.responses[error]
response = self.message_layer.reliability_response(message, response)
response = self.message_layer.matcher_response(response)
# log.msg("Send Error")
return response, host, port
elif message is not None:
# ACK or RST
# log.msg("Received ACK or RST")
self.message_layer.handle_message(message)
return None