序列化C ++对象以通过套接字发送到Python-最佳方法? [英] Serialize C++ object to send via sockets to Python - best approach?

查看:144
本文介绍了序列化C ++对象以通过套接字发送到Python-最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在两个不同的框架之间创建网络通信,一个框架使用 C ++ 编写,另一个框架使用 Python 编写

I need to create a network communication between two different frameworks, one written in C++ and the other in Python.

要交换数据,我想在 C ++ 中创建某种灵活的结构(基本上是一个结构),序列化后,通过套接字发送到 Python ,然后反序列化。

To exchange data, I want to create some kind of flexible structure (basically a struct) in C++, which is serialised, sent through sockets to Python and then deserialised.

最常见的方法是?我确信 Boost 可以在任一侧上做到这一点,因为有 boost python ,但是我没有想炸毁项目需求。因此,除了指定自己的二进制数据格式之外,也许还有较小的库或其他优雅的解决方案吗?

What is the most common way to do this? I'm sure that Boost could do it on either side, since there is boost python, but I don't want to blow up the project requirements that much. So is there maybe a smaller library or whatever another elegant solution except specifying an own binary data format?

UPDATE:

所以这是一个示例,说明如何使用Googles protobuf C ++ 脚本通过 UDP 转换为 Python 脚本。

So here is one example how to use Googles protobuf to send a data structure from a C++ script to a Python script via UDP. This is tested on Mac OS X Mavericks but should work fine on other Unix systems too.

安装protobuf

当然,第一步是安装 protobuf 库。我将自制用于主库,并使用 pip 安装 Python 模块:

The first step is of course installing the protobuf library. I used homebrew for the main library and pip to install the Python modules:

brew install protobuf
pip install protobuf

然后我使用原始语法定义了一个非常简单的数据结构:

Then I defined a very simple data structure using the proto-syntax:


文件名:foo.proto



package prototest;

message Foo {
  required int32 id = 1;
  required string bar = 2;
  optional string baz = 3;
}

该原型文件现在可以通过以下方式转换为C ++和Python类: / p>

This proto-file can now be translated into C++ and Python classes via:

protoc foo.proto --cpp_out=. --python_out=.

该文件夹现在应包含C ++头文件和源文件以及Python代码:

The folder should now contain the C++ header and source files and the Python code:

├── foo.pb.cc
├── foo.pb.h
├── foo.proto
└── foo_pb2.py

让我们看一下非常基本的 C ++ 代码,用于使用UDP在网络上发送 foo 的实例(到端口5555上的localhost):

Let's have a look at the very basic C++ code, which is meant to send an instance of foo over the network, using UDP (to localhost on port 5555):


文件名:send.cc



#include <sys/socket.h>
#include <arpa/inet.h>

// this is our proto of foo
#include "foo.pb.h"

int main(int argc, char **argv)
{
  struct sockaddr_in addr;

  addr.sin_family = AF_INET;
  inet_aton("127.0.0.1", &addr.sin_addr);
  addr.sin_port = htons(5555);

  // initialise a foo and set some properties
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  prototest::Foo foo;
  foo.set_id(4);
  foo.set_bar("narf");

  // serialise to string, this one is obvious ; )    
  std::string buf;
  foo.SerializeToString(&buf);

  int sock = socket(PF_INET, SOCK_DGRAM, 0);
  sendto(sock, buf.data(), buf.size(), 0, (struct sockaddr *)&addr, sizeof(addr));

  return 0;
}

我通过 clang ++

clang++ -o send send.cc foo.pb.cc -lprotobuf

最后,这是 Python 代码,它等待UDP数据包并反序列化放入 foo 。再次:不会进行任何错误检查,这只是为了演示功能:

And finally, this is the Python code, which waits for UDP packets and deserialise them into foo. Again: no error checking whatsoever, this is only to demonstrate the functionality:


文件名:receive.py



import socket
from foo_pb2 import Foo

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 5555))

foo = Foo()
while True:
    data, addr = sock.recvfrom(1024)
    foo.ParseFromString(data)
    print("Got foo with id={0} and bar={1}".format(foo.id, foo.bar))

现在我们完成了,这是最终的目录结构:

Now we're done and this is the final directory structure:

├── foo.pb.cc
├── foo.pb.h
├── foo.proto
├── foo_pb2.py
├── receive.py
├── send
└── send.cc

要测试脚本,只需运行 receive.py 来监听UDP数据包通过

To test the script, simply run receive.py to listen to UDP packets via

python receive.py

并保持眼睛在执行C ++生成的 send 脚本时的输出上:

and keep your eyes on the output when you execute the C++ generated send script:

./send


推荐答案

选择协议缓冲区-Google代码,该资源具有足够的资源用于的问题。您可以创建一个兼容的结构,该结构可以用两种语言读取。

Go for Protocol Buffers - Google Code which has enough resource for c++ and python. You can make a compatible structure which is readable in both languages.


协议缓冲区是一种序列化结构化数据的方法。这样,
在开发程序以通过导线彼此通信或存储数据时很有用。该方法涉及描述某些数据结构的接口
描述语言和一个
程序,该程序从该描述源代码中以各种
编程语言生成用于生成或解析$的字节流。 b $ b代表结构化数据。

这篇关于序列化C ++对象以通过套接字发送到Python-最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆