简体   繁体   English

将C ++中的原始字符串数据传递给Python程序

[英]Passing raw string data in C++ to a Python program

Before simply answering "use SWIG" or "just write it to a file on disk" read this: 在简单回答“使用SWIG”或“仅将其写入磁盘上的文件”之前,请阅读以下内容:

I have a C++ client/server program. 我有一个C ++客户端/服务器程序。 The client requests data from the server. 客户端从服务器请求数据。

The server generates data and calls various routines to check the data. 服务器生成数据并调用各种例程以检查数据。 If all the data is correct, it sends the data via socket to the client. 如果所有数据正确,它将通过套接字将数据发送到客户端。

What I'd like to do is take this raw data (say it was a String type data) and replace my "various routines" with python programs. 我想做的是获取此原始数据(例如,它是String类型的数据),并用python程序替换“各种例程”。

To do this, the raw data will be passed to the python programs to be parsed and checked, then they will need to return a pass/fail value to the C++ server. 为此,原始数据将被传递到python程序进行解析和检查,然后它们需要将通过/失败值返回给C ++服务器。

Any ideas? 有任何想法吗?

That's quite backwards from the normal way of doing things, isn't it! 这是从正常的做事方式倒过来的,不是吗!

You have two main options: 您有两个主要选择:

  1. Keep the Python scripts as they are, run them using fork()/exec() or whatever is appropriate to your platform, and parse their output in C++; 保持Python脚本不变,使用fork()/exec()或适合您平台的任何脚本运行它们,并使用C ++解析它们的输出;
  2. Use CPython's well-documented API to embed it in your C++ program, and invoke the scripts that way. 使用CPython 记录良好的API将其嵌入到C ++程序中,然后以这种方式调用脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM