简体   繁体   English

运行应接受C程序自变量的python脚本

[英]Running python script that should accepts arguments from a C program

how can i specify arguments to a python script from C program , where this arguments must be passed while the calling python script inside the c program 我如何从C程序中指定python脚本的参数,在c程序中调用python脚本时必须传递此参数

This C code is able to run python script successfully ,but how can i pass arguments as well that can be accepted by python script? 此C代码能够成功运行python脚本,但是我又如何传递python脚本可以接受的参数呢?

    #include <stdio.h>
    #include <string.h>
    #include <python2.7/Python.h>
    #include <getopt.h>

      int main (int argc, char * argv[])
      {
        char command[50] = "python2.7  /alok/analyze.py";
        system(command);return(0);
      }

From the commends, i saw that your real problem is, how to make a string from 2 given strings. 从表扬中,我看到了您真正的问题是,如何从2个给定的字符串中制成一个字符串。

What you can do is: Write a function that concatenate 2 strings in to one. 您可以做的是:编写一个将2个字符串连接为一个的函数。 For that you need to get the length of both strings, then add this lengths (also add 1 for the '\\0'-Byte and check for overflows), then use malloc() to reserve buffer space for the new string and copy both strings to this buffer. 为此,您需要获取两个字符串的长度,然后添加此长度(还要为'\\ 0'-Byte添加1并检查溢出),然后使用malloc()为新字符串保留缓冲区空间并复制两者字符串到此缓冲区。

You can do it like this (do not just use this, it is not very well testet, and the error handling is not great): 您可以像这样进行操作(不要仅仅使用它,它不是很好的testet,并且错误处理也不是很好):

void die(const char *msg)
  {
    fprintf(stderr,"[ERROR] %s\n",msg);
    exit(EXIT_FAILURE);
  }


char *catString(const char *a, const char *b)
  {
    //calculate the buffer length we need
    size_t lena = strlen(a);
    size_t lenb = strlen(b);
    size_t lenTot = lena+lenb+1; //need 1 extra for the end-of-string '\0'
    if(lenTot<lena) //check for overflow
      {
        die("size_t overflow");
      }
    //reseve memory
    char *buffer = malloc(lenTot);
    if(!buffer) //check if malloc fail
      {
        die("malloc fail");
      }
    strcpy(buffer,a); //copy string a to the buffer
    strcpy(&buffer[lena],b);//copy string b to the buffer
    return buffer;
  }

After this you can use this function to create the string you need from your static string "python2.7 ./myScript " and argv[1] 之后,您可以使用此函数从静态字符串"python2.7 ./myScript "argv[1]创建所需的字符串。

int main(int argc, char **argv)
  {
    //without a argument we should not call the python script
    if(argc<2)
      {
        die("need at least one argument");
      }
    //make one string to call system()
    char *combined = catString("python2.7 ./myScript ",argv[1]);
    printf("DEBUG complete string is '%s'\n",combined);
    int i = system(combined);
    //we must free the buffer after use it or we generate memory leaks
    free(combined);
    if(i<0)
      {
        die("system()-call failed");
      }
    printf("DEBUG returned from system()-call\n");
    return EXIT_SUCCESS;
  }

You need the extra space in "python2.7 ./myScript " , without you would get "python2.7 ./myScriptArgumentToMain" . 您需要在"python2.7 ./myScript "额外的空间,而不会得到"python2.7 ./myScriptArgumentToMain"

And with this your caller can execute any code he like because we do not escape argv[1] , so a call to your program with yourProgram "argumentToPython ; badProgram argumentToBadProgram" will execute badProgram also which you do not want (in the most cases) 有了这个,您的调用者就可以执行他喜欢的任何代码,因为我们不会对argv[1]转义,因此,使用yourProgram "argumentToPython ; badProgram argumentToBadProgram"来调用您的程序也会执行badProgram ,这也是您不希望的(在大多数情况下)

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

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