1

I have an interactive console application and I need to work with it using Python (send commands and receive output). The application is started by another one, I can't start it from Python script.

Is it possible to connect to already running console application and get access to its stdin/stdout?

Ideally the solution should work both in Windows and Unix, but just Windows version would also be helpful. Currently I am using the solution found here http://code.activestate.com/recipes/440554/ but it doesn't allow connecting to existing process.

Thanks for any input,

  • Do you want to take over control of the stdin/stdout? Observe them only? What do you want to do after you have connected to it? – aychedee Jan 18 '13 at 12:30
  • @aychedee Yes, I need to take control over stdin/stdout. The simplest example of the situation I have would be already running cmd.exe programm, which I need to connect to and send commands (like dir, copy, etc.) and receive output. – Gennadiy Alpaev Jan 21 '13 at 09:04
  • I have the same problem. I have a program started in a win32 console by some other program and now I need to read the screen - just a write it in a log. No need to actually type into the console, just read – 576i Oct 31 '18 at 20:07

1 Answers1

0

Have you considered using sockets since they are straight forward for simple/streaming. They are also platform independent.

The most critical point is thread safety where having to pass IO streams between threads/processes tends to be hectic.

If on the other hand you use a socket, a lot can be communicated without adding too much complexity to how the processes work(coding an error prone RPC for instance).

try Documentation or

example

korefn
  • 955
  • 6
  • 17
  • I don't think sockets can be used to connect to a local process (like cmd.exe for instance). – Gennadiy Alpaev Jan 21 '13 at 12:55
  • Try [this](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) but its not clear whether you want a console(cmd.exe) to communicate with you or your application on cmd.exe to communicate with this new process. The later could be used with sockets. The first, not sure how. – korefn Jan 22 '13 at 06:22