Pipes. Let's start from something simple.
Again, something on Raspberry Pi (Raspbian/Debian), but there is a chance it would be useful for other systems.
I needed to direct a stream of data from c program to python script. To try the easiest way, I did small c program to send some data (string for this example) and another c program (to receive and print) and forced them to co-operate. Below is an explanation of how it is done.
This is easy and often used form of creating a pipe (yes, we will use pipe to stream data from output of 'sender' to input of 'receiver'). Sender will print something out and receiver will accept this data. We will start from both programs being written in c, and then we will create a script in python to do the job of receiver.
1. pipesender.c
2. pipereceiver.c
Compile both programs.
Last bit is to form a pipe. In terminal type:
'sudo' allows running the programs, and ./ tells "Look for the program in the current directory"
This should produce an answer like below:
Received string is: Message from sender.
This simple mechanism allows to use data from c program in python script. Now, lets create python script to show on the screen what was received from sender. We will use the same compiled pipesender.c program to provide some data.
Python script will look like this:
pythonreceiver.py
Its time to try it. In terminal type:
And the result is:
Python receiver received:
Message from sender.
I needed to direct a stream of data from c program to python script. To try the easiest way, I did small c program to send some data (string for this example) and another c program (to receive and print) and forced them to co-operate. Below is an explanation of how it is done.
C sender to c receiver
This is easy and often used form of creating a pipe (yes, we will use pipe to stream data from output of 'sender' to input of 'receiver'). Sender will print something out and receiver will accept this data. We will start from both programs being written in c, and then we will create a script in python to do the job of receiver.
1. pipesender.c
#include <stdio.h> int main(){ printf("Message from sender\n"); return 0; }
2. pipereceiver.c
#include <stdio.h> int main(){ char str1[21]; //buffer long enough for string from sender scanf("%21[\n]", str1); printf("Received string is: %s\n", str1); return 0; }
Compile both programs.
Last bit is to form a pipe. In terminal type:
sudo ./pipesender | ./pipereceiver
'sudo' allows running the programs, and ./ tells "Look for the program in the current directory"
This should produce an answer like below:
Received string is: Message from sender.
C sender to python receiver
This simple mechanism allows to use data from c program in python script. Now, lets create python script to show on the screen what was received from sender. We will use the same compiled pipesender.c program to provide some data.
Python script will look like this:
pythonreceiver.py
def main(args): k = input() #use raw_input() for python versions lower than 3 print("Python receiver received: ") print(k) return 0 if __name__ == '__main__': import sys sys.exit(main(sys.argv))
Its time to try it. In terminal type:
sudo ./pipesender | python pythonreceiver.py
And the result is:
Python receiver received:
Message from sender.
Comments
Post a Comment