Friday, 15 May 2015

python - how to call an interactive program and show output immediately -



python - how to call an interactive program and show output immediately -

i need phone call interactive programme in process , print output while process running. far, i'm doing function:

def call(command): process = subprocess.popen(command, shell=true, stdout=subprocess.pipe, stderr=subprocess.stdout) while true: line = process.stdout.readline().rstrip().encode("utf-8") if line == '': break print(line) process.wait() homecoming process.returncode

the problem is, interactive programme may wait on user input , not add together new line after question, example:

authentication realm: ...> ...

username:

after "username:", there no new line , programme expects user input, code not show "username:".

instead of readline() i'd need functions bytesavailable , read(size) there no such function bytesavaiable().

do need capture output of programme yourself, or need displayed user? if needs displayed, consider letting process write straight stdout:

process = subprocess.popen(command, shell=true)

this neatly avoids need hack own pass-through solution.

if do need capture programme output, can phone call .read() without specifying size. it'll block until has info read (or until stream finished, in case it'll homecoming empty string), won't wait until end of line return, unlike .readline().

python python-3.x subprocess

No comments:

Post a Comment