How to automate interactive console applications in Python?
<p>I want to control running process/program by script in Python. 
I have a program `linphonecÂ´ (You can install: apt-get install linphonec).
My task is:</p>

<ol>
<li>Run <code>linphonec</code> (I'm using subprocess at the moment)</li>
<li>When <code>linphonec</code> is running it has many commands to control this and I want to e.g use <code>proxy list</code> (command in <code>linphonec</code>).</li>
</ol>

<p>Simple flow:</p>

<pre><code>test@ubuntu$ &gt; linphonec
linphonec &gt; proxy list
</code></pre>

<p>How can I do this?</p>

<p>There are actually 2 ways to communicate:</p>

<p>Run your program with <code>myprogram.py | linphonec</code> to pass everything you <code>print</code> to linphonec</p>

<p>Use <a href="https://docs.python.org/3/library/subprocess.html#popen-constructor" rel="nofollow">subprocess.Popen</a> with <a href="https://docs.python.org/3/library/subprocess.html#subprocess.PIPE" rel="nofollow">subprocess.PIPE</a> in constructor via keywrod-args for stdin (propably stdout and stderr, too) and then <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">communicate</a> for a single command or use <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdin" rel="nofollow">stdin</a> and <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdout" rel="nofollow">stdout</a> (<a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stderr" rel="nofollow">stderr</a>) as files</p>

<pre><code>import subprocess
p=subprocess.Popen("linphonec",
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True) #this is for text communication
p.stdin.write("proxy list\n")
result_first_line=p.stdout.readline()
</code></pre>

python linux shell subprocess linphone