且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asyncio 和 pyzmq - 'utf-8' 编解码器无法解码位置 0 的字节 0xff

更新时间:2022-10-21 17:16:54

Zeromq uses the ZMTP protocol. It is a binary protocol so you won't be able to decode it directly.

If you're curious about it, check the ZMTP frames using wireshark and the ZMTP plugin:

You can see that the bytes you got actually corresponds to the greeting message signature.


In order to receive the messages from a ZMQ socket in asyncio, use a dedicated project like aiozmq:

import aiozmq
import asyncio

async def main(port=5555):
    bind = "tcp://*:%s" % port
    rep = await aiozmq.create_zmq_stream(aiozmq.zmq.REP, bind=bind)
    message, = await rep.read()
    print(message.decode())
    rep.write([message])

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()