在Flask应用上解析Protobuf时出现截断的消息

时间:2019-01-07 03:56:32

标签: python flask protocol-buffers

我正在尝试实现protobuf POST消息的简单解析。运行Python 2.7,仅尝试将消息的人类可读字符串转储到文件中。但是,当我尝试此操作时,它会抛出_DecodeError('Truncated message.')

我的代码:

@app.route('/<uid>', methods=['GET', 'POST'])
def handler(uid, event=None, context=None):
    if request.method == 'POST':
        raw_data = request.get_data()
        myfile = open('data.txt', 'w+')
        devices_frame = DevicesFrame_pb2.DevicesFrame()
        devices_frame.ParseFromString(str(raw_data))
        myfile.write(str(devices_frame))
        myfile.close()
        return 200

错误:

Traceback (most recent call last):
  File "c:\python27\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\python27\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\python27\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "main.py", line 427, in handler
    devices_frame.ParseFromString(str(raw_data))
  File "c:\python27\lib\site-packages\google\protobuf\message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "c:\python27\lib\site-packages\google\protobuf\internal\python_message.py", line 1083, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "c:\python27\lib\site-packages\google\protobuf\internal\python_message.py", line 1109, in InternalParse
    new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
  File "c:\python27\lib\site-packages\google\protobuf\internal\decoder.py", line 850, in SkipField
    return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
  File "c:\python27\lib\site-packages\google\protobuf\internal\decoder.py", line 782, in _SkipFixed64
    raise _DecodeError('Truncated message.')

编辑:由于某种原因,它现在抛出Tag had invalid wire type

0 个答案:

没有答案