通过python运行tcl脚本

时间:2014-05-28 11:11:38

标签: python-2.7 tcl

我的自动化脚本是使用Python完成的,我必须控制spirent Testcenter(它只支持tcl)。我可以通过Tcl脚本与思博伦沟通,如果我通过python调用相同的脚本我得到以下错误

TCL文件 - Stc1.tcl

package require SpirentTestCenter
source [ file join [ file dirname [ info script ] ] {ipv6_max_session_header.tcl} ]
puts "SpirentTestCenter system version:\t[stc::get system1 -Version]"

Python文件 - tcl_py.py

import Tkinter
import os

r=Tkinter.Tk()
r.tk.eval('source Stc_1.tcl')

我得到的错误 -

C:\Python27\MyScripts>python tcl_py.py
Traceback (most recent call last):
  File "tcl_py.py", line 5, in <module>
    r.tk.eval('source Stc_1.tcl')
_tkinter.TclError: can't find package SpirentTestCenter

2 个答案:

答案 0 :(得分:0)

作为python子进程的替代方法,您可以做的其他事情是在python脚本中打开localhost SSH会话并在那里打开tcl shell(使用expect或paramiko shell)。

答案 1 :(得分:0)

根据需要,您可以通过多种方式使用子进程在 python 界面中运行您的 Tcl 脚本。例如,运行带参数的命令并返回其输出。

import subprocess
p = subprocess.check_output("script.tcl", shell=True)
print(p)