IPC

brianc at temple.edu brianc at temple.edu
Wed Jul 28 12:03:56 EDT 2004


Unfortunately your problem seems to be very specific to the
application you are trying to communicate with. You can only
read from the stdout if the program has actually written to
it. Sometimes programs won't execute until EOF has been reached. 

I was reading the Pexpect project this morning as someone
posted a link to it in reply to your original request. There
is a good bit there explaining about the pitfalls of working
with interactive programs.

Sorry I can't be of much more help. Trying to reverse engineer
the program is probably the only way you'll be able to acheive
your goal. 

-Brian

---- Original message ----
>Date: Wed, 28 Jul 2004 11:39:43 -0400
>From: "Yannick Turgeon" <nobody at nowhere.com>  
>Subject: Re: IPC  
>To: python-list at python.org
>
>Thanks Brian for your help.
>
>It seems to have something fundamental that I don't
understand. I've
>installed process.py (first link) and it's almost doing what
I want. The
>problem is that I cannot read stdout before closing stdin.
How come? Aren't
>they fully independant? I try to read only one byte at a time
and it's
>waiting that byte forever. As if the child process did not
receive the
>income data yet to process it. I did execute stdin.flush().
So I suppose the
>process received the command ("?\n" in ftp program) and
produce the output,
>but I cannot read it.
>
>An alternative that I thought about, is to close the stdin in
order to read
>stdout and the recreate the stdin for this process. Is that
faisible? I've
>got the PID, I should be able to create a pipe with the stdin
of that
>process.
>
>I haven't been able to make your solution with select.select
work. After
>looking to the doc, it's specified that this function can be
use only with
>socket when running on Windows. The second link don't work
for Windows
>neither.
>
>Any feedback would be appreciate.
>
>Yannick
>
>
><brianc at temple.edu> wrote in message
>news:mailman.856.1090955408.5135.python-list at python.org...
>> There's two modules that I like to use for this kind of stuff:
>> http://starship.python.net/crew/tmick/ (process.py)
>> http://www.lysator.liu.se/~astrand/popen5/ (also process.py
>> just to be confusing)
>>
>> Hopefully one of these will make it into the standard library.
>> I honestly like them both and combine parts from both when I
>> need to create my own classes.
>>
>> The reason your program hangs is because .readlines() will
>> keep reading untill an EOF(ie: program exits) is reached and
>> since the pipe is still open it never will be. (The example
>> given in the popen2 module,
>> http://docs.python.org/lib/popen2-flow-control.html, eludes to
>> this but never actually says it.)
>>
>> I recently did a quick hack using select and popen2, but I
>> doubt it's fast or even safe, but it works.
>>
>> #yadda yadda
>> r, w, e = popen2.popen3(cmd)
>> cmd = "?\n"
>> w.write(cmd)
>> w.flush()
>> #continuely test for output to read
>> while select.select([e],[],[],1)[0]:#timeout guessing game
>>     line=e.readline()
>>     #do something with line
>> while select.select([r],[],[],1)[0]:#timeout guessing game
>>     line=r.readline()
>>     #do something with line
>>
>> If you're output is line buffered you probably shouldn't have
>> many problems with this approach if you can be assured your
>> program will respond within the 1 second alloted to it. If
>> you're positive you'll get output, just remove the timeout.
>>
>> Hope this helps.
>>
>> -Brian
>>
>>
>> ---- Original message ----
>> >Date: Tue, 27 Jul 2004 14:20:19 -0400
>> >From: "Yannick Turgeon" <nobody at nowhere.com>
>> >Subject: Re: IPC
>> >To: python-list at python.org
>> >
>> >Larry,
>> >
>> >As I said, I use FTP only to test the IPC and give here a
>> known example.
>> >It's in fact with a custom program that I have to communicate.
>> >
>> >Any help in this regard?
>> >
>> >Yannick
>> >
>> >"Larry Bates" <lbates at swamisoft.com> wrote in message
>> >news:e9OdnbY0ddteC5vcRVn-sw at comcast.com...
>> >> Python has built in support for ftp (see ftplib)
>> >> use it instead of trying to "communicate" with
>> >> external FTP program.
>> >>
>> >> http://www.python.org/doc/current/lib/module-ftplib.html
>> >>
>> >> You can catch any exceptions (like failure to connect)
>> >> by using python try:/except: blocks.
>> >>
>> >> HTH,
>> >> Larry Bates
>> >> Syscon, Inc.
>> >>
>> >> "Yannick Turgeon" <nobody at nowhere.com> wrote in message
>> >> news:Z6wNc.21572$i_2.899460 at news20.bellglobal.com...
>> >> > Hi,
>> >> >
>> >> > I'm relatively new to Python. I'm using 2.3.4 on W2K.
>> >> >
>> >> > What I want to do is to start a program and interact with
>> it. Say my
>> >> program
>> >> > is FTP, I want to start FTP then send the commande "open
>> x.x.x.x" then
>> >> look
>> >> > for the answer (if the connection is opened or not), then
>> do something
>> >> > dependant of the success or error.
>> >> >
>> >> > I tried with popen3. The problem I got with this: it
>> seems that I have
>> >to
>> >> > end the program before being able to read the output. Or
>> maybe I'm not
>> >> using
>> >> > it correctly. I do test the communication with FTP exec.
>> but it will be
>> >a
>> >> > custom program in real. Here is my code:
>> >> >
>> >> >
>> >> >     def test(self):
>> >> >         cmd = "ftp"
>> >> >         r, w, e = popen2.popen3(cmd)
>> >> >
>> >> >         cmd = "?\n"            # A simple FTP commande
>> >> >         w.write(cmd)
>> >> >         w.flush()
>> >> >
>> >> >         # That is what I would like but it's hanging
>> here. I have to
>> >> remove
>> >> > this group and read at the end.
>> >> >         for line in e.readlines():
>> >> >             # Do something conditionnal to the result of
>> "line"
>> >> >             pass
>> >> >         for line in r.readlines():
>> >> >             # Do something conditionnal to the result of
>> "line"
>> >> >             pass
>> >> >
>> >> >
>> >> >         cmd = "quit\n"
>> >> >         w.write(cmd)
>> >> >         w.flush()
>> >> >
>> >> >         for line in e.readlines():
>> >> >             print line
>> >> >         for line in r.readlines():
>> >> >             print line
>> >> >
>> >> >         w.close()
>> >> >         r.close()
>> >> >         e.close()
>> >> > ----------------------
>> >> >
>> >> > Anybody can help? Thanks for your time.
>> >> >
>> >> > Yannick
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>> >--
>> >http://mail.python.org/mailman/listinfo/python-list
>
>
>-- 
>http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list