def start(self):
"""Starts receiving messages on the underlying socket and passes them
to the message router.
"""
self._is_running = True
while self._is_running:
try:
zmq_msg = await self._socket.recv_multipart()
message = Message()
message.ParseFromString(zmq_msg[-1])
await self._msg_router.route_msg(message)
except DecodeError as e:
LOGGER.warning('Unable to decode: %s', e)
except zmq.ZMQError as e:
LOGGER.warning('Unable to receive: %s', e)
return
except asyncio.CancelledError:
self._is_running = False
python类DecodeError()的实例源码
def _parse_header(cls, header_proto, resource):
"""Deserializes a resource's base64 encoded Protobuf header.
"""
header = header_proto()
try:
header_bytes = base64.b64decode(resource['header'])
header.ParseFromString(header_bytes)
except (KeyError, TypeError, ValueError, DecodeError):
header = resource.get('header', None)
LOGGER.error(
'The validator sent a resource with %s %s',
'a missing header' if header is None else 'an invalid header:',
header or '')
raise errors.ResourceHeaderInvalid()
resource['header'] = cls._message_to_dict(header)
return resource
def handle(self, connection_id, message_content):
response_proto = client_batch_submit_pb2.ClientBatchSubmitResponse
def make_response(out_status):
return HandlerResult(
status=HandlerStatus.RETURN,
message_out=response_proto(status=out_status),
message_type=Message.CLIENT_BATCH_SUBMIT_RESPONSE)
try:
request = client_batch_submit_pb2.ClientBatchSubmitRequest()
request.ParseFromString(message_content)
except DecodeError:
return make_response(response_proto.INTERNAL_ERROR)
for batch in request.batches:
if batch.trace:
LOGGER.debug("TRACE %s: %s", batch.header_signature,
self.__class__.__name__)
if not all(map(is_valid_batch, request.batches)):
return make_response(response_proto.INVALID_BATCH)
return HandlerResult(status=HandlerStatus.PASS)
def handle(self, connection_id, message_content):
"""Handles parsing incoming requests, and wrapping the final response.
Args:
connection_id (str): ZMQ identity sent over ZMQ socket
message_content (bytes): Byte encoded request protobuf to be parsed
Returns:
HandlerResult: result to be sent in response back to client
"""
try:
request = self._request_proto()
request.ParseFromString(message_content)
except DecodeError:
LOGGER.info('Protobuf %s failed to deserialize', request)
return self._wrap_result(self._status.INTERNAL_ERROR)
try:
response = self._respond(request)
except _ResponseFailed as e:
response = e.status
return self._wrap_result(response)
def _parse_main_response(self, response_raw, subrequests):
self.log.debug('Parsing main RPC response...')
if response_raw.status_code == 403:
raise ServerSideAccessForbiddenException("Seems your IP Address is banned or something else went badly wrong...")
elif response_raw.status_code == 502:
raise ServerBusyOrOfflineException("502: Bad Gateway")
elif response_raw.status_code != 200:
error = 'Unexpected HTTP server response - needs 200 got {}'.format(response_raw.status_code)
self.log.warning(error)
self.log.debug('HTTP output: \n%s', response_raw.content.decode('utf-8'))
raise UnexpectedResponseException(error)
if response_raw.content is None:
self.log.warning('Empty server response!')
return False
response_proto = ResponseEnvelope()
try:
response_proto.ParseFromString(response_raw.content)
except message.DecodeError as e:
self.log.warning('Could not parse response: %s', e)
return False
self.log.debug('Protobuf structure of rpc response:\n\r%s', response_proto)
try:
self.log.debug('Decode raw over protoc (protoc has to be in your PATH):\n\r%s', self.decode_raw(response_raw.content).decode('utf-8'))
except:
self.log.debug('Error during protoc parsing - ignored.')
response_proto_dict = protobuf_to_dict(response_proto)
response_proto_dict = self._parse_sub_responses(response_proto, subrequests, response_proto_dict)
return response_proto_dict
def extract_channel_config(configtx_proto_envelope):
""" Extracts the protobuf 'ConfigUpdate' object out ouf the 'ConfigEnvelope'.
Args:
configtx_proto_envelope (common_pb2.Envelope): The encoded bytes of the
ConfigEnvelope protofbuf.
Returns:
config_update (configtx_pb2.ConfigUpadeEnvelope.config_update):
The encoded bytes of the ConfigUpdate protobuf, ready to be signed
Raises:
ValueError: If there is an error in protobuf_decode due to a wrong or
not valid profobuf file a ValueError is raised.
"""
_logger.debug('extract_channel_config - start')
try:
envelope = common_pb2.Envelope()
envelope.ParseFromString(configtx_proto_envelope)
payload = common_pb2.Payload()
payload.ParseFromString(envelope.payload)
configtx = configtx_pb2.ConfigUpdateEnvelope()
configtx.ParseFromString(payload.data)
except DecodeError as e:
_logger.error('extract_channel_config - an error occurred decoding'
' the configtx_proto_envelope: {}'.format(e))
raise ValueError('The given configtx_proto_envelope was not valid: {}'
.format(e))
return configtx.config_update
def read_bundle_file(bundle_file):
# Read in bundle file.
bundle = generator_pb2.GeneratorBundle()
with tf.gfile.Open(bundle_file, 'rb') as f:
try:
bundle.ParseFromString(f.read())
except message.DecodeError as e:
raise GeneratorBundleParseException(e)
return bundle
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def read_blocks_from_queue(self):
"""Returns a generator of the blocks in the queue.
Override this method if you wish to change the queue (blocks transformation)
form.
Yields:
Each yield is a single block object (block_pb2.Block).
"""
message_lines = []
for line in sys.stdin:
if constants.QUEUE_DELIMITER in line:
block = block_pb2.Block()
try:
block.ParseFromString('\n'.join(message_lines))
except message_mod.DecodeError:
sys.stderr.write(
'ERROR: Can not read protocol buffer from queue. Is '
'human_readable perhaps set to true? I am not a human. '
'Aborting...\n')
sys.exit(-1)
yield block
message_lines = []
else:
message_lines.append(line.rstrip('\n'))
message_test.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def testAssertOversizeProto(self):
from google.protobuf.pyext._message import SetAllowOversizeProtos
SetAllowOversizeProtos(False)
q = self.proto_cls()
try:
q.ParseFromString(self.p_serialized)
except message.DecodeError as e:
self.assertEqual(str(e), 'Error parsing message')
reflection_test.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def testParseTruncated(self):
# This test is only applicable for the Python implementation of the API.
if api_implementation.Type() != 'python':
return
first_proto = unittest_pb2.TestAllTypes()
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
for truncation_point in range(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
pos = second_proto._InternalParse(serialized, 0, truncation_point)
# If we didn't raise an error then we read exactly the amount expected.
self.assertEqual(truncation_point, pos)
# Parsing to unknown fields should not throw if parsing to known fields
# did not.
try:
pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
self.assertEqual(truncation_point, pos2)
except message.DecodeError:
self.fail('Parsing unknown fields failed when parsing known fields '
'did not.')
except message.DecodeError:
# Parsing unknown fields should also fail.
self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
serialized, 0, truncation_point)
def testParseTruncated(self):
# This test is only applicable for the Python implementation of the API.
if api_implementation.Type() != 'python':
return
first_proto = unittest_pb2.TestAllTypes()
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
for truncation_point in xrange(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
pos = second_proto._InternalParse(serialized, 0, truncation_point)
# If we didn't raise an error then we read exactly the amount expected.
self.assertEqual(truncation_point, pos)
# Parsing to unknown fields should not throw if parsing to known fields
# did not.
try:
pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
self.assertEqual(truncation_point, pos2)
except message.DecodeError:
self.fail('Parsing unknown fields failed when parsing known fields '
'did not.')
except message.DecodeError:
# Parsing unknown fields should also fail.
self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
serialized, 0, truncation_point)
def testParseTruncated(self):
# This test is only applicable for the Python implementation of the API.
if api_implementation.Type() != 'python':
return
first_proto = unittest_pb2.TestAllTypes()
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
for truncation_point in xrange(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
pos = second_proto._InternalParse(serialized, 0, truncation_point)
# If we didn't raise an error then we read exactly the amount expected.
self.assertEqual(truncation_point, pos)
# Parsing to unknown fields should not throw if parsing to known fields
# did not.
try:
pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
self.assertEqual(truncation_point, pos2)
except message.DecodeError:
self.fail('Parsing unknown fields failed when parsing known fields '
'did not.')
except message.DecodeError:
# Parsing unknown fields should also fail.
self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
serialized, 0, truncation_point)
def testAssertOversizeProto(self):
from google.protobuf.pyext._message import SetAllowOversizeProtos
SetAllowOversizeProtos(False)
q = self.proto_cls()
try:
q.ParseFromString(self.p_serialized)
except message.DecodeError as e:
self.assertEqual(str(e), 'Error parsing message')
def testParseTruncated(self):
# This test is only applicable for the Python implementation of the API.
if api_implementation.Type() != 'python':
return
first_proto = unittest_pb2.TestAllTypes()
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
for truncation_point in range(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
pos = second_proto._InternalParse(serialized, 0, truncation_point)
# If we didn't raise an error then we read exactly the amount expected.
self.assertEqual(truncation_point, pos)
# Parsing to unknown fields should not throw if parsing to known fields
# did not.
try:
pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
self.assertEqual(truncation_point, pos2)
except message.DecodeError:
self.fail('Parsing unknown fields failed when parsing known fields '
'did not.')
except message.DecodeError:
# Parsing unknown fields should also fail.
self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
serialized, 0, truncation_point)
def do_test(request):
test_message = test_messages_proto3_pb2.TestAllTypes()
response = conformance_pb2.ConformanceResponse()
test_message = test_messages_proto3_pb2.TestAllTypes()
try:
if request.WhichOneof('payload') == 'protobuf_payload':
try:
test_message.ParseFromString(request.protobuf_payload)
except message.DecodeError as e:
response.parse_error = str(e)
return response
elif request.WhichOneof('payload') == 'json_payload':
try:
json_format.Parse(request.json_payload, test_message)
except Exception as e:
response.parse_error = str(e)
return response
else:
raise ProtocolError("Request didn't have payload.")
if request.requested_output_format == conformance_pb2.UNSPECIFIED:
raise ProtocolError("Unspecified output format")
elif request.requested_output_format == conformance_pb2.PROTOBUF:
response.protobuf_payload = test_message.SerializeToString()
elif request.requested_output_format == conformance_pb2.JSON:
try:
response.json_payload = json_format.MessageToJson(test_message)
except Exception as e:
response.serialize_error = str(e)
return response
except Exception as e:
response.runtime_error = str(e)
return response
python_message.py 文件源码
项目:bigmuddy-network-telemetry-collector
作者: cisco
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def _AddMergeFromStringMethod(message_descriptor, cls):
"""Helper for _AddMessageMethods()."""
def MergeFromString(self, serialized):
length = len(serialized)
try:
if self._InternalParse(serialized, 0, length) != length:
# The only reason _InternalParse would return early is if it
# encountered an end-group tag.
raise message_mod.DecodeError('Unexpected end-group tag.')
except IndexError:
raise message_mod.DecodeError('Truncated message.')
except struct.error as e:
raise message_mod.DecodeError(e)
return length # Return this for legacy reasons.
cls.MergeFromString = MergeFromString
local_ReadTag = decoder.ReadTag
local_SkipField = decoder.SkipField
decoders_by_tag = cls._decoders_by_tag
def InternalParse(self, buffer, pos, end):
self._Modified()
field_dict = self._fields
while pos != end:
(tag_bytes, new_pos) = local_ReadTag(buffer, pos)
field_decoder = decoders_by_tag.get(tag_bytes)
if field_decoder is None:
new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
if new_pos == -1:
return pos
pos = new_pos
else:
pos = field_decoder(buffer, new_pos, end, self, field_dict)
return pos
cls._InternalParse = InternalParse
def _parse_main_response(self, response_raw, subrequests):
self.log.debug('Parsing main RPC response...')
if response_raw.status_code == 403:
raise ServerSideAccessForbiddenException("Seems your IP Address is banned or something else went badly wrong...")
elif response_raw.status_code == 502:
raise ServerBusyOrOfflineException("502: Bad Gateway")
elif response_raw.status_code != 200:
error = 'Unexpected HTTP server response - needs 200 got {}'.format(response_raw.status_code)
self.log.warning(error)
self.log.debug('HTTP output: \n%s', response_raw.content.decode('utf-8'))
raise UnexpectedResponseException(error)
if response_raw.content is None:
self.log.warning('Empty server response!')
return False
response_proto = ResponseEnvelope()
try:
response_proto.ParseFromString(response_raw.content)
except message.DecodeError as e:
self.log.warning('Could not parse response: %s', e)
return False
self.log.debug('Protobuf structure of rpc response:\n\r%s', response_proto)
try:
self.log.debug('Decode raw over protoc (protoc has to be in your PATH):\n\r%s', self.decode_raw(response_raw.content).decode('utf-8'))
except:
self.log.debug('Error during protoc parsing - ignored.')
response_proto_dict = protobuf_to_dict(response_proto)
response_proto_dict = self._parse_sub_responses(response_proto, subrequests, response_proto_dict)
return response_proto_dict
def _fetch_current_frame(self):
path = '{}/{}'.format(self.PLUGIN_LOGDIR, SUMMARY_FILENAME)
try:
frame = read_tensor_summary(path).astype(np.uint8)
self.most_recent_frame = frame
return frame
except (message.DecodeError, IOError, tf.errors.NotFoundError):
return self.most_recent_frame
def read_tensor_summary(path):
with tf.gfile.Open(path, 'rb') as summary_file:
summary_string = summary_file.read()
if not summary_string:
raise message.DecodeError('Empty summary.')
summary_proto = tf.Summary()
summary_proto.ParseFromString(summary_string)
tensor_proto = summary_proto.value[0].tensor
array = tf.make_ndarray(tensor_proto)
return array
def _parse_main_response(self, response_raw, subrequests):
self.log.debug('Parsing main RPC response...')
if response_raw.status_code == 403:
raise ServerSideAccessForbiddenException("Seems your IP Address is banned or something else went badly wrong...")
elif response_raw.status_code == 502:
raise ServerBusyOrOfflineException("502: Bad Gateway")
elif response_raw.status_code != 200:
error = 'Unexpected HTTP server response - needs 200 got {}'.format(response_raw.status_code)
self.log.warning(error)
self.log.debug('HTTP output: \n%s', response_raw.content.decode('utf-8'))
raise UnexpectedResponseException(error)
if response_raw.content is None:
self.log.warning('Empty server response!')
return False
response_proto = ResponseEnvelope()
try:
response_proto.ParseFromString(response_raw.content)
except message.DecodeError as e:
self.log.warning('Could not parse response: %s', e)
return False
self.log.debug('Protobuf structure of rpc response:\n\r%s', response_proto)
try:
self.log.debug('Decode raw over protoc (protoc has to be in your PATH):\n\r%s', self.decode_raw(response_raw.content).decode('utf-8'))
except:
self.log.debug('Error during protoc parsing - ignored.')
response_proto_dict = protobuf_to_dict(response_proto)
response_proto_dict = self._parse_sub_responses(response_proto, subrequests, response_proto_dict)
return response_proto_dict
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def testAssertOversizeProto(self):
from google.protobuf.pyext._message import SetAllowOversizeProtos
SetAllowOversizeProtos(False)
q = self.proto_cls()
try:
q.ParseFromString(self.p_serialized)
except message.DecodeError as e:
self.assertEqual(str(e), 'Error parsing message')
def testParseTruncated(self):
# This test is only applicable for the Python implementation of the API.
if api_implementation.Type() != 'python':
return
first_proto = unittest_pb2.TestAllTypes()
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
for truncation_point in range(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
pos = second_proto._InternalParse(serialized, 0, truncation_point)
# If we didn't raise an error then we read exactly the amount expected.
self.assertEqual(truncation_point, pos)
# Parsing to unknown fields should not throw if parsing to known fields
# did not.
try:
pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
self.assertEqual(truncation_point, pos2)
except message.DecodeError:
self.fail('Parsing unknown fields failed when parsing known fields '
'did not.')
except message.DecodeError:
# Parsing unknown fields should also fail.
self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
serialized, 0, truncation_point)
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
while True:
try:
response = api_req(service, api_endpoint, access_token, *args,
**kwargs)
if response:
return response
debug('retrying_api_req: api_req returned None, retrying')
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
def _parse_response(proto, response):
"""Parses the content from a validator response Message.
"""
try:
content = proto()
content.ParseFromString(response.content)
return content
except (DecodeError, AttributeError):
LOGGER.error('Validator response was not parsable: %s', response)
raise errors.ValidatorResponseInvalid()
def handle(self, connection_id, message_content):
response_proto = client_batch_submit_pb2.ClientBatchSubmitResponse
def make_response(out_status):
return HandlerResult(
status=HandlerStatus.RETURN,
message_out=response_proto(status=out_status),
message_type=Message.CLIENT_BATCH_SUBMIT_RESPONSE)
try:
request = client_batch_submit_pb2.ClientBatchSubmitRequest()
request.ParseFromString(message_content)
for batch in request.batches:
if batch.trace:
LOGGER.debug("TRACE %s: %s", batch.header_signature,
self.__class__.__name__)
if not all(
self._verifier.check_off_chain_batch_roles(batch)
for batch in request.batches):
return make_response(response_proto.INVALID_BATCH)
if not all(
self._verifier.is_batch_signer_authorized(batch)
for batch in request.batches):
return make_response(response_proto.INVALID_BATCH)
except DecodeError:
return make_response(response_proto.INTERNAL_ERROR)
return HandlerResult(status=HandlerStatus.PASS)