简体   繁体   中英

Python controlling Tcl shell

Is there a way in Python to launch a Tcl shell or a unix shell and take control of it to perform operations? I understand that the subprocess, TkInter can be used to execute shell/Tcl scripts and get the output.

Specifically this will be a wrapper on the Tcl shell/tool that will control its stdin/out. I have tried establishing a client-server connection but in order for python to control tcl shell, the connection needs to be established manually. What should be approach here?

With Tkinter, you have a Tcl interpreter inside the same process as Python. That's fine for doing conventional stuff (or Tk) but if you've got some sort of custom Tcl interpreter to control, you'll probably want to launch that as a subprocess connected by pipes to the parent Python process.

The subprocess module in Python has most of what you'll need.

import subprocess

p = subprocess.Popen(["tclimplementation"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# Turn off buffering on the Tcl side
print("fconfigure stdout -buffering none", file=p.stdin)
# Also should read from p.stdout; here's how
line = p.stdout.readline().strip()

That telling of the Tcl side to fconfigure stdout -buffering none is important, as Tcl (in common with most processes) defaults to buffering multiple kilobytes when writing to anything other than a terminal (great for performance, not great for interactive use).

Now that you can send messages back and forth, you need to think more about how to actually control the other side. That's rather application specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM