品易云推流 关闭
文章详情页
文章 > Python基础教程 > python中Pexpect的工作流程

python中Pexpect的工作流程

头像

小妮浅浅

2021-04-27 10:15:525403浏览 · 0收藏 · 0评论

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、工作流程步骤

(1)用spawn来执行一个程序;

(2)用expect方法来等待指定的关键字,这个关键字是被执行的程序打印到标准输出上面的;

(3)当发现这个关键字以后,使用send/sendline方法发送字符串给这个程序。

2、实例

spawn类

class spawn(SpawnBase):
    '''This is the main class interface for Pexpect. Use this class to start
    and control child applications. '''
 
    # This is purely informational now - changing it has no effect
    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学习指路:python基础教程

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图