简体   繁体   English

如何从python传回一个值到c ++并返回?

[英]how to pass a value to c++ from python and back?

i would like to pass values from python to a c++ program for an encryption from inside a python program and then return the value from there to the python program . 我想将值从python传递到c ++程序,以便从python程序内部进行加密,然后将值从那里返回到python程序。 how to do it? 怎么做?

If you want to use some existing Unix-style command line utility that reads from stdin and writes to stdout, you can use subprocess.Popen by using Popen.communicate() : 如果你想使用一些现有的Unix风格的命令行工具,从标准输入读取和写入到stdout,你可以使用subprocess.Popen通过Popen.communicate()

import subprocess

p = subprocess.Popen(["/your/app"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = p.communicate(input)[0]

As said msw in the other post, the proper solution is using PyObject. 正如另一篇文章中所说的msw,正确的解决方案是使用PyObject。 If you want to have a two-way communication between C++ & Python, Boost Python would be interesting for you. 如果你想在C ++和Python之间进行双向通信,Boost Python对你来说会很有趣。 Take a look at website Boost Python , 看看Boost Python网站,

This post would also be interesting: How to expose a C++ class to Python without building a module 这篇文章也很有趣: 如何在不构建模块的情况下将C ++类公开给Python

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

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