I'm writing a C++ program, which 'll run Python 3 interpreter as a child process, then connect two Linux anonymous pipes - one to it's stdout
and stderr
and the second to interpreter's stdin
; next communicate with it via these channels.
I need to run Python in interactive mode, i.e. pass one command to it using input pipe and wait for the answer on the output pipe. Everything 'd be fine, but it seems Python can run interactive mode only if it's stdout
and stdin
connected to tty's.
Python docs quotation:
The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.
Indeed when I'm running interpreter with pipes instead of tty, I can't see anything in response pipe, after the command was sent.
So - can I workaround such behaviour by some way and make python3 interpreter works exactly the way it was started from terminal by user?
Again, the problem in a nutshell:
I need to integrate Python to my C++ server app, to allow clients execute python commands. Embedding interpreter into server looks like a bad idea, mainly for reasons of safety ( users can damage server or it's data, besides server is running with some privileges I won't wanna grant to users ).
Another possible solution is to use interpreter in CLI way ( command mode). The main problem is - then I need import some modules and pre-execute some code to provide my server environment and some API to users. It 'll be too heavy to do it before calling interpreter with every command ( these actions are quite complex, including network connection establishing )
So, running interpreter in a separate process and server communicating with it using IPC mechanisms seems not so bad idea.
Anyway, I'll be glad to take a look at your suggestions If you have any.