Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过gevent websocket发送二进制数据_Python_Sockets_Websocket_Compression - Fatal编程技术网

Python 通过gevent websocket发送二进制数据

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

我需要通过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.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)))