带参数运行python脚本 [英] Run a python script with arguments
问题描述
我要调用从C Python脚本,传递所需要的脚本一些参数。
我想使用的脚本mrsync,或多个远端同步。我得到这个命令行工作,通过调用:
蟒蛇mrsync.py -m /tmp/targets.list -s / tmp目录/ sourcedata -t的/ tmp / targetdata
-m是含有靶IP位址列表。
-s是包含要同步的文件的目录。
-t是那里的文件将被放置在目标机器的目录。
块引用>到目前为止,我设法不带参数运行Python脚本,使用下面的C程序:
Py_Initialize();
FILE *文件=的fopen(/ tmp目录/ myfile.py,R);
PyRun_SimpleFile(文件/tmp/myfile.py);
Py_Finalize();这工作正常。但是,我找不到我怎么可以通过这些参数传递给
PyRun_SimpleFile(..)
方法。解决方案好像你正在寻找使用Python开发的API从Python.h一个答案。下面是你应该工作的例子:
#我叫mypy.py python脚本
进口SYS如果len(sys.argv中)= 2!
sys.exit(没有足够的ARGS)
ca_one = STR(sys.argv中[1])
ca_two = STR(sys.argv中[2])打印命令我行参数是+ ca_one +和+ ca_two和那么C code通过这些ARGS:
//我的code文件
#包括LT&;&stdio.h中GT;
#包括LT&; python2.7 / Python.h>无效的主要()
{
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);
文件= FOPEN(mypy.py,R);
PyRun_SimpleFile(文件mypy.py);
Py_Finalize(); 返回;
}如果你可以传递参数到C函数这一任务变得更加容易:
无效的主要(INT ARGC,CHAR *的argv [])
{
FILE *文件; Py_SetProgramName(的argv [0]);
Py_Initialize();
PySys_SetArgv(ARGC,ARGV);
文件= FOPEN(mypy.py,R);
PyRun_SimpleFile(文件mypy.py);
Py_Finalize(); 返回;
}您可以只通过这些直通。现在我的解决方案,只用2命令行参数的时间的缘故,但你可以使用,你需要通过所有6相同的概念......当然还有简洁的方式来捕捉蟒蛇侧ARGS太多,但这仅仅是基本的想法。
希望它帮助!
I want to call a Python script from C, passing some arguments that are needed in the script.
The script I want to use is mrsync, or multicast remote sync. I got this working from command line, by calling:
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.
So far I managed to run a Python script without parameters, by using the following C program:
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.解决方案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:
#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:
//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:
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.
Hope it helps!
这篇关于带参数运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!