Python Gevent Websocket客户端因回传的协议缓冲消息出现错误“解码错误:截断消息”

2024-10-01 09:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的websocket服务器,它将所有消息回送给客户机。在

import gevent
from geventwebsocket.resource import WebSocketApplication
from geventwebsocket.server import WebSocketServer
from geventwebsocket.resource import Resource
import ams_pb2

class AMSWebSocketServer(WebSocketApplication):
    def __init__(self, ws):
        super(AMSWebSocketServer, self).__init__(ws)
        pass

    def on_open(self):
        pass

    def on_message(self, message):
        print 'received message'
        print message
        if message is None:
            print 'message none'
            return

        print 'echo message back'
        self.ws.send(message)

    def on_close(self, reason):
        print "connection closed"
        gevent.sleep(0)


resource = Resource({'/': AMSWebSocketServer})

服务器是使用gunicorn命令生成的

^{pr2}$

我有一个测试客户机,它发送websocket消息以进行回显

from ws4py.client.threadedclient import WebSocketClient
import ams_pb2


class DummySwitch(WebSocketClient):
    def closed(self, code, reason=None):
        pass

    def received_message(self, msg):
        if msg is None:
            print 'none'
            return
        print 'received message'
        ams_message = ams_pb2.AMSConfig()
        ams_message.ParseFromString(msg)
        print ams_message
        print msg


if __name__ == '__main__':
    end_point = 'ws://127.0.0.1:9000'

    client = DummySwitch(
        end_point,
        headers=[
        ]
    )
    client.connect()

    print 'sending message'

    AMSConfig = ams_pb2.AMSConfig()
    AMSConfig.CliConfig = True
    print AMSConfig
    msg = AMSConfig.SerializeToString()

    #msg = 'Hello'
    print msg

    client.send(msg)

    client.run_forever()

我的protobuff文件是: 包装ams

message AMSConfig {
  optional bool CliConfig = 1;
}

每当我的客户机向服务器发送protobuff消息时,我都能看到它在服务器中被解析,但是当服务器向客户机回送相同的消息时,客户机由于以下原因而失败:

文件“客户_测试.py“,第15行,在接收到的\u消息中 ams公司_message.ParseFromString(消息) 文件“/usr/lib/python2.6/site-packages/google/protobuf/消息.py“,第186行,在ParseFromString中 self.MergeFromString(序列化) 文件“/usr/lib/python2.6/site-packages/google/protobuf/internal/python_消息.py“,第847行,在MergeFromString中 引发消息_模解码错误(“截断消息”。) 解码错误:消息被截断。在

发送回给客户机的是一个简单的字符串,我可以看到这个字符串被修改了。但是,客户端无法解析回显的protobuff消息。在

我不明白为什么字符串的回显是有效的,但是对于协议缓冲区,它在我的示例中不起作用。在

谢谢你的帮助。在


Tags: fromimportself服务器client消息message客户机
3条回答

我在客户端遇到了同样的问题,但这可以通过在客户端代码中扩大接收缓冲区大小来解决。例如:

maxpkglen = 1024 * 1024 // original maxpkglen = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(req, (server, port))
rsp, server = s.recvfrom(maxpkglen)
s.close()

在我的客户机程序中也遇到这样的问题,因为我将接收缓冲区设置为硬数字1024。当我把接收缓冲区的大小扩大到10*1024时,这个问题就解决了。也谢谢@lulyon的建议。在

您可能需要一个从对等代码读取所有数据的循环!在

我遇到了这个问题,但对我来说,答案是不要直接从received_message(self, msg)使用msg变量,而是使用msg.data来实际获取与消息相关的数据并从中进行解析。在

相关问题 更多 >