Python 通过gevent websocket发送二进制数据
我需要通过websocket(两端的python API)发送一个包含几个大熊猫数据帧的pickled python字典。为了提高性能,我想在通过websocket发送之前压缩pickle对象。但是,执行此操作时,收到的消息始终为“无” 客户:Python 通过gevent websocket发送二进制数据,python,sockets,websocket,compression,Python,Sockets,Websocket,Compression,我需要通过websocket(两端的python API)发送一个包含几个大熊猫数据帧的pickled python字典。为了提高性能,我想在通过websocket发送之前压缩pickle对象。但是,执行此操作时,收到的消息始终为“无” 客户: df = pd.DataFrame({'a':[1,2,3,4]}) d = dict(b=df) msg = zlib.compress(pickle.dumps(d),5) socket.send(msg) 服务器: msg = socket.re
df = pd.DataFrame({'a':[1,2,3,4]})
d = dict(b=df)
msg = zlib.compress(pickle.dumps(d),5)
socket.send(msg)
服务器:
msg = socket.receive()
# msg is always None when called with client code above.
data = pickle.loads(zlib.decompress(msg))
有更好的方法吗?
我正在通过Flask Socket框架使用gevent websocket。要在Python 2.7中发送二进制数据,必须在客户端(ws4py)中设置
binary=True
:
另一个注意事项是,在接收端,在发送到zlib.decompress()之前,需要将消息包装在缓冲区中
socket.send(msg, Binary=True)
msg = socket.receive()
data = pickle.loads(zlib.decompress(buffer(msg)))