python中Pexpect的工作流程
本文教程操作環境:windows7系統、Python3.9.1,DELLG3電腦。
1、工作流程步驟
(1)用spawn來執行一個程序;
(2)用expect方法來等待指定的關鍵字,這個關鍵字是被執行的程序打印到標準輸出上面的;
(3)當發現這個關鍵字以后,使用send/sendline方法發送字符串給這個程序。
2、實例
spawn類
classspawn(SpawnBase):
'''ThisisthemainclassinterfaceforPexpect.Usethisclasstostart
andcontrolchildapplications.'''
#Thisispurelyinformationalnow-changingithasnoeffect
use_native_pty_fork=use_native_pty_fork
def__init__(self,command,args=[],timeout=30,maxread=2000,
searchwindowsize=None,logfile=None,cwd=None,env=None,
ignore_sighup=False,echo=True,preexec_fn=None,
encoding=None,codec_errors='strict',dimensions=None,
use_poll=False):
通過spawn()方法用來執行一個程序,返回程序的操作句柄,后續就可以通過操作句柄來與這個程序進行交互了。
以上就是python中Pexpect工作流程的介紹,希望能對大家有所幫助。更多Python學習教程請關注IT培訓機構:千鋒教育。