python中的脚本和交互式终端客户端
我需要使用python为交互式终端客户端编写脚本/实现自动化。客户端接受三个参数并按如下方式运行:python中的脚本和交互式终端客户端,python,Python,我需要使用python为交互式终端客户端编写脚本/实现自动化。客户端接受三个参数并按如下方式运行: >./myclient <arg1> <arg2> <arg3> Welcome... blah... blah.. [user input] some more blah... blah... for the input entered blah.. blah.. [basically it accepts input and puts the out
>./myclient <arg1> <arg2> <arg3>
Welcome...
blah...
blah..
[user input]
some more blah... blah... for the input entered
blah..
blah..
[basically it accepts input and puts the output in the console until the user types 'quit']
./myclient
欢迎
废话。。。
废话。。
[用户输入]
还有一些废话。。。废话。。。对于输入的输入
废话。。
废话。。
[基本上,它接受输入并将输出放在控制台中,直到用户键入“退出”]
现在,我需要在python中自动执行此操作,并将控制台输出保存在一个文件中
非常感谢您在这方面提供的任何帮助……您可以查看一下
示例代码:
import cmd
import sys
class Prompt(cmd.Cmd):
def __init__(self, stufflist=[]):
cmd.Cmd.__init__(self)
self.prompt = '>>> '
self.stufflist = stufflist
print "Hello, I am your new commandline prompt! 'help' yourself!"
def do_quit(self, arg):
sys.exit(0)
def do_print_stuff(self, arg):
for s in self.stufflist:
print s
p = Prompt(sys.argv[1:])
p.cmdloop()
$ python cmdtest.py foo bar
Hello, I am your new commandline prompt! 'help' yourself!
>>> help
Undocumented commands:
======================
help print_stuff quit
>>> print_stuff
foo
bar
>>> quit
示例测试:
import cmd
import sys
class Prompt(cmd.Cmd):
def __init__(self, stufflist=[]):
cmd.Cmd.__init__(self)
self.prompt = '>>> '
self.stufflist = stufflist
print "Hello, I am your new commandline prompt! 'help' yourself!"
def do_quit(self, arg):
sys.exit(0)
def do_print_stuff(self, arg):
for s in self.stufflist:
print s
p = Prompt(sys.argv[1:])
p.cmdloop()
$ python cmdtest.py foo bar
Hello, I am your new commandline prompt! 'help' yourself!
>>> help
Undocumented commands:
======================
help print_stuff quit
>>> print_stuff
foo
bar
>>> quit
为了将输出保存到文件中,您可以将通常转到stdout的内容也写入到文件中,例如使用以下类:
class Tee(object):
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
def write(self, s):
self.out1.write(s)
self.out2.write(s)
def flush(self):
self.out1.flush()
self.out2.flush()
您可以这样使用它:
with open('cmdtest.out', 'w') as f:
# write stdout to file and stdout
t = Tee(f, sys.stdout)
sys.stdout = t
一个问题是,通过stdin读入的命令没有出现在这个输出中,但我相信这可以很容易地解决。您可以看看
示例代码:
import cmd
import sys
class Prompt(cmd.Cmd):
def __init__(self, stufflist=[]):
cmd.Cmd.__init__(self)
self.prompt = '>>> '
self.stufflist = stufflist
print "Hello, I am your new commandline prompt! 'help' yourself!"
def do_quit(self, arg):
sys.exit(0)
def do_print_stuff(self, arg):
for s in self.stufflist:
print s
p = Prompt(sys.argv[1:])
p.cmdloop()
$ python cmdtest.py foo bar
Hello, I am your new commandline prompt! 'help' yourself!
>>> help
Undocumented commands:
======================
help print_stuff quit
>>> print_stuff
foo
bar
>>> quit
示例测试:
import cmd
import sys
class Prompt(cmd.Cmd):
def __init__(self, stufflist=[]):
cmd.Cmd.__init__(self)
self.prompt = '>>> '
self.stufflist = stufflist
print "Hello, I am your new commandline prompt! 'help' yourself!"
def do_quit(self, arg):
sys.exit(0)
def do_print_stuff(self, arg):
for s in self.stufflist:
print s
p = Prompt(sys.argv[1:])
p.cmdloop()
$ python cmdtest.py foo bar
Hello, I am your new commandline prompt! 'help' yourself!
>>> help
Undocumented commands:
======================
help print_stuff quit
>>> print_stuff
foo
bar
>>> quit
为了将输出保存到文件中,您可以将通常转到stdout的内容也写入到文件中,例如使用以下类:
class Tee(object):
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
def write(self, s):
self.out1.write(s)
self.out2.write(s)
def flush(self):
self.out1.flush()
self.out2.flush()
您可以这样使用它:
with open('cmdtest.out', 'w') as f:
# write stdout to file and stdout
t = Tee(f, sys.stdout)
sys.stdout = t
一个问题是,通过stdin读入的命令没有出现在这个输出中,但我相信这可以很容易地解决。您可能想要使用(这是古老的expect的纯python版本)
import-pexpect
proc=pexpect.spawn(“./myclient”)
proc.logfile=要使用的日志文件
expect(['告诉您myclient正在等待输入的字符串'])
proc.sendline('要发送给myclient的行')
proc.expect(['要等待的另一行']))
进程sendline('quit')#用于myclient退出
过程预期([pexpect.EOF])
这样的事情应该足以解决你的问题。不过,pexpect的功能要多得多,所以请阅读文档以了解更高级的用例。您可能想要使用它(这是古老的expect的纯python版本)
import-pexpect
proc=pexpect.spawn(“./myclient”)
proc.logfile=要使用的日志文件
expect(['告诉您myclient正在等待输入的字符串'])
proc.sendline('要发送给myclient的行')
proc.expect(['要等待的另一行']))
进程sendline('quit')#用于myclient退出
过程预期([pexpect.EOF])
这样的事情应该足以解决你的问题。但是,pexpect能够提供更多功能,因此请阅读文档以了解更高级的用例。我尝试了Popen,但无法有效运行终端客户端。我也无法使用communicate()发送多个输入。当我使用stdin=PIPE,然后使用stdin.read发送数据时,客户端表现得很古怪,我尝试了Popen,但无法有效地运行终端客户端。我也无法使用communicate()发送多个输入。当我使用stdin=PIPE,然后使用stdin.read发送数据时,客户端表现得很奇怪。谢谢。这很有魅力。但现在我有另一个问题。我正在向客户端发送两个输入
proc.sendline('input1')time.sleep(60)proc.sendline('input2')
,但问题是它没有等待。它似乎不考虑线程而发送输入。sleep(60)…通常最好不要在发送数据之前尝试睡眠,而是从另一个进程中查找某种确认消息。谢谢。这很有魅力。但现在我有另一个问题。我正在向客户端发送两个输入proc.sendline('input1')time.sleep(60)proc.sendline('input2')
,但问题是它没有等待。它似乎不考虑线程而发送输入。sleep(60)…通常最好不要在发送数据之前尝试睡眠,而是从另一个进程中寻找某种确认消息。如果有人投减号票,这将非常糟糕。虽然这可能不是这个问题的最佳解决方案,但它是一个信息量大、实用性强的答案,显示了解决这个问题的其他选项(因此,我再次投票赞成零)。@Jan PhilipGehrcke仅仅因为有人提出问题并接受了答案,并不意味着其他答案是错误的。:)我还发现你的回答很有启发性。@SunSparc我认为这是一个反对票,而不是不被接受:-)被人投反对票是非常糟糕的。虽然这可能不是这个问题的最佳解决方案,但它是一个信息量大、实用性强的答案,显示了解决这个问题的其他选项(因此,我再次投票赞成零)。@Jan PhilipGehrcke仅仅因为有人提出问题并接受了答案,并不意味着其他答案是错误的。:)我还发现你的回答很有启发性。@SunSparc我认为这是一个反对票,而不是不被接受:-)