0

I want to continuously receive stdout and stderr(optional) of a program. The following is the stdout of the program:

got 3847 / 0 / 0 / 0 pkts/drops/pktinfisue/crpts with 40.6859 Mbps during 8.166 sec
timestamp: 3412618016 0 

got 3842885 / 0 / 0 / 0 pkts/drops/pktinfisue/crpts with 40.6424 Gbps during 1.00052 sec
timestamp: 3412700516 55

got 4190413 / 0 / 0 / 0 pkts/drops/pktinfisue/crpts with 44.3178 Gbps during 1.00041 sec
timestamp: 3412792016 116

So far using pipes:

#include <iostream>
#include <string>
#include <unistd.h>
#include <stdexcept>
#include <python3.7m/Python.h>

using namespace std;

string exec(const char* cmd) {
    char buffer[40];
    string result = "";
    FILE* pipe = popen(cmd, "r");
    if (!pipe) throw runtime_error("popen() failed!");
    try {
        while (fgets(buffer, sizeof buffer, pipe) != NULL) {
            c++;
            result += buffer;
            cout<<buffer<<endl;
        }
    } catch (...) {
        pclose(pipe);
        throw;
    }
    pclose(pipe);
    return result;

}

int main()
{
    char *dirr;
    dirr = "/home/user/receiver";
    int chdir_return_value;
    chdir_return_value = chdir(dirr);
    exec("sudo ./rx_hello_world");

    return 0;
}

i think i am able to get the data in different lines like this:

got 3847 / 0 / 0 / 0 pkts/drops/p
ktinfisue/crpts with 40.6859 Gbps durin
g 8.166 sec

timestamp: 3412618016 0

Now i want to send these data to a Python script so that i can parse and analyze the data. for example, i want to get the average of the 40.6859 Mbps say after every 10 seconds or so.

Any help regarding sending these data to python so that i can parse these numbers easily will be a great help.

codeCat
  • 17
  • 10
  • Why cannot you leave the piping to the shell? – dedObed May 13 '20 at 12:09
  • @dedObed, sorry i dont follow!!! Can you please explain as i am new to this topic? – codeCat May 13 '20 at 12:36
  • Instead of running `./my-cpp-tool` which has to do terrible stuff to run a Python script, it would feel more natural to run `./my-cpp-tool | data-collector.py`, allowing both the tools to focus just on their thing. – dedObed May 13 '20 at 17:32

1 Answers1

1

You are looking for Popen class of the subprocess module in python.

Python equivalent of the C function could be along the lines of

from subprocess import Popen, PIPE


def exec(*args):
    with Popen(args, stdout=PIPE) as proc:
        while proc.poll() is None:
            print(proc.stdout.read(40))
        print(proc.stdout.read())

As an alternative solution, you could also wrap the C code in python and call the C API from python. There are several resources online on how to do that.

  • Actually i have gather the data from the *stdout* now i just want to a python script for analyzing only. Anyways, Thank you for your help. – codeCat May 13 '20 at 14:44