使用参数运行python脚本

时间:2021-02-14 07:13:17

I want to call a Python script from C, passing some arguments that are needed in the script.

我想从C调用一个Python脚本,传递脚本中需要的一些参数。

The script I want to use is mrsync, or multicast remote sync. I got this working from command line, by calling:

我想使用的脚本是mrsync或多播远程同步。通过调用

python mrsync.py -m /tmp/targets.list -s /tmp/sourcedata -t /tmp/targetdata

-m is the list containing the target ip-addresses. -s is the directory that contains the files to be synced. -t is the directory on the target machines where the files will be put.

-m是包含目标ip地址的列表。-s是包含要同步的文件的目录。-t是目标机器上存放文件的目录。

So far I managed to run a Python script without parameters, by using the following C program:

到目前为止,我使用以下C程序成功地运行了一个没有参数的Python脚本:

Py_Initialize();
FILE* file = fopen("/tmp/myfile.py", "r");
PyRun_SimpleFile(file, "/tmp/myfile.py");
Py_Finalize();

This works fine. However, I can't find how I can pass these argument to the PyRun_SimpleFile(..) method.

这是很好。但是,我无法找到如何将这些参数传递给PyRun_SimpleFile(.. .)方法。

2 个解决方案

#1


33  

Seems like you're looking for an answer using the python development APIs from Python.h. Here's an example for you that should work:

看起来您正在寻找一个使用python开发api的答案。这里有一个你应该工作的例子:

#My python script called mypy.py
import sys

if len(sys.argv) != 2:
  sys.exit("Not enough args")
ca_one = str(sys.argv[1])
ca_two = str(sys.argv[2])

print "My command line args are " + ca_one + " and " + ca_two

And then the C code to pass these args:

然后C码传递这些args:

//My code file
#include <stdio.h>
#include <python2.7/Python.h>

void main()
{
    FILE* file;
    int argc;
    char * argv[3];

    argc = 3;
    argv[0] = "mypy.py";
    argv[1] = "-m";
    argv[2] = "/tmp/targets.list";

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

If you can pass the arguments into your C function this task becomes even easier:

如果可以将参数传递到C函数中,这个任务就会变得更简单:

void main(int argc, char *argv[])
{
    FILE* file;

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

You can just pass those straight through. Now my solutions only used 2 command line args for the sake of time, but you can use the same concept for all 6 that you need to pass... and of course there's cleaner ways to capture the args on the python side too, but that's just the basic idea.

你可以直接通过。为了节省时间,我的解决方案只使用了2个命令行args,但是对于所有需要传递的6个命令行,您都可以使用相同的概念……当然,也有更简单的方法来捕获python端上的args,但这只是基本的想法。

Hope it helps!

希望它可以帮助!

#2


8  

You have two options.

你有两个选择。

  1. Call

    调用

    system("python mrsync.py -m /tmp/targets.list -s /tmp/sourcedata -t /tmp/targetdata")
    

    in your C code.

    在你的C代码。

  2. Actually use the API that mrsync (hopefully) defines. This is more flexible, but much more complicated. The first step would be to work out how you would perform the above operation as a Python function call. If mrsync has been written nicely, there will be a function mrsync.sync (say) that you call as

    实际使用mrsync定义的API。这更灵活,但要复杂得多。第一步是计算如何作为Python函数调用执行上述操作。如果mrsync写得很好,就会有一个函数mrsync。同步(比如说)你调用as

    mrsync.sync("/tmp/targets.list", "/tmp/sourcedata", "/tmp/targetdata")
    

    Once you've worked out how to do that, you can call the function directly from the C code using the Python API.

    一旦您解决了如何做到这一点,您就可以使用Python API直接从C代码调用函数。

#1


33  

Seems like you're looking for an answer using the python development APIs from Python.h. Here's an example for you that should work:

看起来您正在寻找一个使用python开发api的答案。这里有一个你应该工作的例子:

#My python script called mypy.py
import sys

if len(sys.argv) != 2:
  sys.exit("Not enough args")
ca_one = str(sys.argv[1])
ca_two = str(sys.argv[2])

print "My command line args are " + ca_one + " and " + ca_two

And then the C code to pass these args:

然后C码传递这些args:

//My code file
#include <stdio.h>
#include <python2.7/Python.h>

void main()
{
    FILE* file;
    int argc;
    char * argv[3];

    argc = 3;
    argv[0] = "mypy.py";
    argv[1] = "-m";
    argv[2] = "/tmp/targets.list";

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

If you can pass the arguments into your C function this task becomes even easier:

如果可以将参数传递到C函数中,这个任务就会变得更简单:

void main(int argc, char *argv[])
{
    FILE* file;

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypy.py","r");
    PyRun_SimpleFile(file, "mypy.py");
    Py_Finalize();

    return;
}

You can just pass those straight through. Now my solutions only used 2 command line args for the sake of time, but you can use the same concept for all 6 that you need to pass... and of course there's cleaner ways to capture the args on the python side too, but that's just the basic idea.

你可以直接通过。为了节省时间,我的解决方案只使用了2个命令行args,但是对于所有需要传递的6个命令行,您都可以使用相同的概念……当然,也有更简单的方法来捕获python端上的args,但这只是基本的想法。

Hope it helps!

希望它可以帮助!

#2


8  

You have two options.

你有两个选择。

  1. Call

    调用

    system("python mrsync.py -m /tmp/targets.list -s /tmp/sourcedata -t /tmp/targetdata")
    

    in your C code.

    在你的C代码。

  2. Actually use the API that mrsync (hopefully) defines. This is more flexible, but much more complicated. The first step would be to work out how you would perform the above operation as a Python function call. If mrsync has been written nicely, there will be a function mrsync.sync (say) that you call as

    实际使用mrsync定义的API。这更灵活,但要复杂得多。第一步是计算如何作为Python函数调用执行上述操作。如果mrsync写得很好,就会有一个函数mrsync。同步(比如说)你调用as

    mrsync.sync("/tmp/targets.list", "/tmp/sourcedata", "/tmp/targetdata")
    

    Once you've worked out how to do that, you can call the function directly from the C code using the Python API.

    一旦您解决了如何做到这一点,您就可以使用Python API直接从C代码调用函数。