1.1.说明:
Wexpect 是一个 Python 模块,用于生成子应用程序并自动控制它们
Wexpect 可用于自动化交互式应用程序,例如 ssh、ftp、passwd、telnet 等
它可用于自动化安装脚本,以在不同服务器上复制软件包安装
它可用于自动化软件测试。Wexpect 本着 Don Libes 的 Expect 的精神,但 Wexpect 是纯 Python
Python 的其他类似 Expect 的模块需要 TCL 和 Expect 或需要编译 C 扩展
Wexpect 不使用 C、Expect 或 TCL 扩展。
原始 Pexpect 应该适用于支持标准 Python pty 模块的任何平台
Wexpect 适用于 Windows 平台。Wexpect 界面注重易用性,让简单的任务变得容易
1.2.Install
pip install wexpect
2.实例:
实例1:To interract with a child process use spawn method:
import wexpect
def test():
child = wexpect.spawn('cmd.exe')
child.expect('>')
child.sendline('ls')
child.expect('>')
print(child.before)
child.sendline('exit')
实例2:
# 它启动一个 Windows 命令解释器(又名 cmd)列出当前目录并退出
def hello_wexpect():
# Start cmd as child process
child = wexpect.spawn('cmd.exe')
# Wait for prompt when cmd becomes ready.
child.expect('>')
# Prints the cmd's start message
print('1.before = ',child.before)
print('2.after = ',child.after)
# run list directory command
child.sendline('ls')
# Waiting for prompt
child.expect('>')
# Prints content of the directory
print('3.before = ',child.before)
print('4.after = ',child.after)
# Exit from cmd
child.sendline('exit')
# Waiting for cmd termination.
child.wait()
'''
C:\ProgramData\Anaconda3\python.exe E:/程序/Python代码/wrap_ha/juejin/OpSql.py
[('future_base',), ('information_schema',), ('mysql',), ('new_futures',), ('performance_schema',), ('sakila',), ('sys',), ('world',)]
1.before = Microsoft Windows [版本 10.0.18362.387](c) 2019 Microsoft Corporation。保留所有权利。
(base) E:\程序\Python代码\wrap_ha\juejin
2.after = >
(base) E:\程序\Python代码\wrap_ha\juejin
4.after = >
'''
实例3.1:
'''
是一个完整的自定义示例代码。此示例脚本运行foo python 程序,并与之通信。
请本地运行 foo首先是 .py,这是一个非常基本的 stdio 处理程序脚本。
'''
import sys
import wexpect
import os
here = os.path.dirname(os.path.realpath(__file__))
# Path of python executable:
pythonInterp = sys.executable
prompt = ': '
# Start the child process
p = wexpect.spawn(pythonInterp, [here + '\\foo.py'])
# Wait for prompt
p.expect(prompt)
print(p.before)
# Send the 'small integer'
p.sendline('3')
p.expect(prompt)
print(p.before)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
# Send the name
p.sendline('Bob')
# wait for program exit.
p.wait()
# print the texts
print(p.read(), end='')
实例3.2:foo.py
'''
This is is a very basic stdio handler script. This is used by python.py example.
'''
import time
# Read an integer from the user:
print('Give a small integer: ', end='')
num = input()
# Wait the given time
for i in range(int(num)):
print('waiter ' + str(i))
time.sleep(0.2)
# Ask the name of the user to say hello.
print('Give your name: ', end='')
name = input()
print('Hello ' + str(name), end='')
实例4:
from __future__ import print_function
import sys
import os
import re
here = os.path.dirname(os.path.abspath(__file__))
wexpectPath = os.path.dirname(here)
import wexpect
# Path of cmd executable:
cmd_exe = 'cmd'
# The prompt should be more sophisticated than just a '>'.
cmdPrompt = re.compile('[A-Z]\:.+>')
# Start the child process
p = wexpect.spawn(cmd_exe)
# Wait for prompt
p.expect(cmdPrompt, timeout = 5)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
while True:
# Wait and run a command.
command = input()
p.sendline(command)
try:
# Wait for prompt
p.expect(cmdPrompt)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
except wexpect.EOF:
# The program has exited
print('The program has exied... BY!')
break
实例5:
def gen_model(self,db_name:str,model_name:str='',user='root',password='root',host='localhost'):
father_path = os.path.abspath(os.path.dirname(os.getcwd()))
model_name = model_name if model_name else db_name +'_model.py'
cmd = r'python -m pwiz -e mysql -u %s -H %s -P %s > %s\peewee_data\%s'%(
user,host,db_name,father_path,model_name)
#python -m pwiz -e mysql -u root -H localhost -P new_futures > E:\程序\Python代码\wrap_ha\peewee_data\new_futures_model.py
child = wexpect.spawn('cmd.exe')
child.expect('>')
child.sendline(cmd)
child.expect(':')
print(child.before)
child.sendline(password)
child.expect('>')
child.sendline('exit')
wexpect · PyPI