Reputation: 35
I have a program named app.exe which takes 1 argument and starts execution.While app.exe is executing,it will prompt me for confirmation such as --"Are you sure you want to continue?"I have to say yes/no.
Now I have written a program in python which would talk to app.exe,providing 1 input parameter.But I am unable to provide the app.exe with yes/no option(For Are you sure you want to continue?) via my script.Below is my program.
proc=Popen(outlines, shell=True, stdin=PIPE, stdout=PIPE)
#Outlines is the command which contains 1 input parameter.
proc.communicate(input='n\n')[0]
#I saw this in python documentation.But it is not working.
Let me know what I am missing here.
Upvotes: 0
Views: 392
Reputation: 23490
proc = Popen(outlines, shell=True, stdin=PIPE, stdout=PIPE)
stdOut = ''
answered = False
while proc.Poll() is None:
stdOut += proc.stdout.read()
if answered is False and 'sure you want to continue' in stdOut:
proc.stdin.write('n\n')
proc.stdin.flush()
answered = True # Not the most beautiful way to solve it, but chuck it.. it works.
proc.stdout.close()
As always, someone will comment on this so i'll go ahead and say this right away.
if you PIPE
stdout or stderr, you need to read from the buffer, otherwise it might get full and that will hang your application unexpectedly. In this case you need to check the output anyway for "are you sure..." so it kind of solves itself, but keep that in mind.
Another thing to note is that for numerous of reasons, using shell=True
is bad and/or dangerous. I've never had issues with it, but it's a fact everyone likes to throw around and I'm sure someone will explain it to me and you one day. or you can just google if you're interested.
If you use while proc.Poll() == None:
you might get an issue if the function would return a 0
, so use is
for comparison to be sure you exit the loop in the proper state.
Don't forget to close your proc.stdout
, if you debug your application and you start to many sessions in a short while there will not be enough filehandles free to open another stdout
(because it counts as a file handle for various reasons).
Since .read()
will obviously hang the application if there's no data to be read, you'll have to poll the object and ask if there is input to fetch.
You can do this with select
if you're using Linux, and on Windows you might use a thread of sort to check and report data.
The cheapskate version to get you going for the time being would be to ignore stdout
after you've answered with \n
even if this isn't recommended really.
proc = Popen(outlines, shell=True, stdin=PIPE, stdout=PIPE)
stdOut = ''
answered = False
while proc.Poll() is None:
if not answered: # This makes all the difference
stdOut += proc.stdout.read()
if answered is False and 'sure you want to continue' in stdOut:
proc.stdin.write('n\n')
proc.stdin.flush()
answered = True
Again, i don't condone this in the long run. But it might give you an idea of why this is happening and also to move along your project so you don't get stuck on a single detail for to long.
Upvotes: 2