[ADD] examples

This commit is contained in:
Benedek Racz 2019-04-24 15:55:26 +02:00
parent d6a8cc5e57
commit 2bd27dc77e
3 changed files with 73 additions and 0 deletions

27
examples/cmd.py Normal file
View File

@ -0,0 +1,27 @@
# A simple example code for wexpect written in python-2.7
from __future__ import print_function
import wexpect
# Path of cmd executable:
cmdPath = 'C:\Windows\System32\cmd.exe'
cmdPrompt = '>'
# Start the child process
p = wexpect.spawn(cmdPath)
# Wait for prompt
p.expect(cmdPrompt)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
# Send a command
p.sendline('ls')
p.expect(cmdPrompt)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')

13
examples/foo.py Normal file
View File

@ -0,0 +1,13 @@
from __future__ import print_function
import time
print('Give a small integer: ', end='')
num = input()
for i in range(num):
print('waiter ' + str(i))
time.sleep(0.2)
print('Give your name: ', end='')
name = raw_input()
print('Hello ' + str(name), end='')

33
examples/python.py Normal file
View File

@ -0,0 +1,33 @@
# A simple example code for wexpect written in python-2.7
from __future__ import print_function
import sys
import wexpect
# Path of python executable:
pythonInterp = sys.executable
prompt = ': '
# Start the child process
p = wexpect.spawn(pythonInterp, ['foo.py'])
# Wait for prompt
p.expect(prompt)
# Send the 'small integer'
p.sendline('3')
p.expect(prompt)
# 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='')