Is it possible to have python open a terminal and write to it?

烂漫一生 提交于 2019-12-23 04:00:07

问题


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!


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/3340341/is-it-possible-to-have-python-open-a-terminal-and-write-to-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!