python 将消息输入到标准输入 | 主进程与子进程命令行来回多次交互 | pexpect模块 | cmd模块 | 应用
cmd模块作为子进程启动:
cmd模块读取标准输入的内容进行交互
import cmd
class RobotCmd(cmd.Cmd):
'模拟robot启动'
doc_header = "输入help [cmd]查看帮助信息"
intro = ' 欢迎使用机器人交互式shell,输入help或?获取支持的功能'
prompt = 'robot> '
stop = False
i=0
def run(self):
self.cmdloop()
def postcmd(self, stop: bool, line: str) -> bool:
return super().postcmd(stop, line)
def do_getAllUsers(self, args):
'''用户重启(重新开启浏览器访问)'''
self.i+=1
print(f'ok {self.i}')
RobotCmd().run()
pexpect模块简单应用:
注意: pexpect只能在linux上使用, windows失效
p = pexpect.spawnu('python3 /root/xxs//r/rb.py')
p.expect('robot> ') # 命令行等待输入的提示
p.sendline(m