我想在 Python 中实现套接字客户端。服务器期望前 8 个字节包含以字节为单位的总传输大小。在 C 客户端中,我这样做了:
uint64_t total_size = zsize + sizeof ( uint64_t );
uint8_t* xmlrpc_call = malloc ( total_size );
memcpy ( xmlrpc_call, &total_size, sizeof ( uint64_t ) );
memcpy ( xmlrpc_call + sizeof ( uint64_t ), zbuf, zsize );
其中 zsize 和 zbuff 是我要传输的大小和数据。在 python 中,我创建这样的字节数组:
cmd="<xml>do_reboot</xml>"
result = deflate (bytes(cmd,"iso-8859-1"))
size = len(result)+8
在 Python 中填充标题的最佳方法是什么?无需将值分隔为 8 个字节并循环复制