c++调用shell & shell调用python

来源:互联网 发布:http默认端口号 编辑:程序博客网 时间:2024/06/16 07:28
  • 本文主要实现c++调用shell & shell调用python

  • c++/cpp2shell.cpp
  • 向shell脚本传入参数,并获得返回值
#include <stdlib.h>#include <iostream>using namespace std;string exec(const char* cmd) {  FILE* pipe = popen(cmd, "r");  if (!pipe) return "ERROR";  char buffer[128];  std::string result ="";  while(!feof(pipe)) {    if(fgets(buffer, 128, pipe) != NULL)    result += buffer;  }  pclose(pipe);  return result;}int main(){  char pic[3][16] = {"6418.jpg","2991.jpg","7319.jpg"};  string shell_str = "sh shell2python.sh ";  for(int i=0;i<3;i++){    string pic_name = pic[i];    string exec_str = shell_str + pic_name;    string result = exec(exec_str.c_str());    printf(result.c_str());  }  return 0;}
  • shell/shell2python.sh
#!/bin/bash a=$(/usr/local/bin/python /home/szx/test/test_captcha.py $1 2>&1)b=${a:0-4:4}  #只取最后4个字符echo $b
  • python/test_captcha.py(部分)
import sys  #...(省略大部分代码)def captcha_test_once(image_name):  image = Image.open(image_name)  image = np.array(image)  image = convert2gray(image)  image = image.flatten() / 255  predict_text = crack_captcha(image)  sys.exit(predict_text)captcha_test_once(sys.argv[1])
原创粉丝点击