views:

88

answers:

2

For example, if I have this code:

subprocess.call(['gnome-terminal'])

Is it possible to have python output strings to that specific terminal that was just opened? Thanks!

+2  A: 

I think that PExpect might do this for you:

Pexpect is basically a pattern matching system. It runs programs and watches output. When output matches a given pattern Pexpect can respond as if a human were typing responses. Pexpect can be used for automation, testing, and screen scraping. Pexpect can be used for automating interactive console applications such as ssh, ftp, passwd, telnet, etc. It can also be used to control web applications via lynx, w3m, or some other text-based web browser. Pexpect is pure Python. Unlike other Expect-like modules for Python Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module.

BrianLy
Your answer works for *reading*, but not for *writing*.
Arafangion
@Arafangion Actually PExpect _DOES_ support writing, and does it well.It can even write to PAM password inputs, which usually do not work with typical stdin. PExpect imitates tty device IIRC
Daniel Kluev
BrianLy
@Daniel: Well, ok, yes, you can use PExpect to do writing, but why would you without doing any reading?
Arafangion
+3  A: 

Possibly, but it is easier to have a custom process running in the subordinate terminal. For example, given sserv.py from the example server in the documentation the command:

 gnome-terminal -e "python ./sserv.py"

will happily chat on port 9999 with you. Given a more complex sserv.py it could do anything you want (anything terminalish, that is).

msw
+1: Tidy. Complete. Simple.
S.Lott