Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Alternatives
============
Why write yet another expect module for Python? After all, there are
several others available. However, none of them are as good as
expect itself, and the design of them means that they cannot easily be
made as good. I felt that a fresh start was needed.
However, the alternatives are not all bad. They all have their strong
points, as this list will show. This list is sorted on release date,
with the most stale project first.
expy
----
Homepage: ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/System
Review based on version: 0.4a
Release date: 1995-08-28
Authors: Saad Mufti and Farzad Farid
Uses Don Libes' expect library: yes
Uses Tkinter: no
Sample usage:
try:
val, spawn = expect.expect([([exp1, exp2], [(expect.glob, 'ftp> ', 1),
(expect.glob, 'Connection timeout', 2)])])
if val == 1:
spawn.send("Goober")
# Go On
elif val == 2:
spawn.send("Foo")
print 'Connection with the server timed out'
except expect.timeout
print 'We must be hung somewhere...'
Why this isn't good enough:
- no support for expect_after and expect_before
- cumbersome syntax
- needs TCL
Why this is better than the rest:
+ does support waiting for output from two processes at once. That
makes this the best expect integration into Python I've seen to
date.
pyexpect.py
-----------
Homepage: ftp://ftp.sunsite.auc.dk/disk1/python/contrib-09-Dec-1999/System/pyexpect.py.Z
Review based on version: pyexpect.py,v 1.2 1996/10/07 23:41:48
Release date: 1996-10-07 (?)
Author: Mitchell S. Chapman
Uses Don Libes' expect library: no
Uses Tkinter: yes
Sample code:
patterns = ("OK", "NO", "BUSY", ".*[Bb]ye$")
status = apply(self.exp.expect, patterns)
if status >= 0:
print "Matched", patterns[status]
else:
print "Status =", status
Why this isn't good enough:
- there is no way to wait for input from more than one process at a
time.
- no support for expect_after and expect_before
- relies on a windows system!
Python-expect aka exploop.py
----------------------------
Homepage: http://theopenlab.uml.edu/pipermail/loci-general/1999-April/000006.html
Review based on version: exploop.py,v 1.5 1997/01/27 19:11:59
Release date: 1997-01-27
Author: Tim O'Malley
Uses Don Libes' expect library: no
Uses Tkinter: no
Sample usage:
exp = Expect(sys.stdin, sys.stdout)
# Create our Patterns
pats = []
pats.append( Pattern(regex.compile("[Pp]ython"), _testCB) )
pats.append( Pattern(regex.compile("s+tar"), _testCB) )
# This pattern uses the default Pattern callback
pats.append( Pattern(regex.compile("[Bb]ye")))
# Output a prompt of sorts
exp.send("\nYou have 20 seconds. Type 'Bye' or 'bye' to exit sooner.\n")
# Call expect()...
try:
matcher = exp.expect(pats, 20)
print "\nGood Bye"
except ExpectTimeout:
print "\nTime has expired"
except ExpectEOF:
print "\nInput closed"
Why this isn't good enough:
- there is no way to wait for input from more than one process at a
time.
- no support for expect_after and expect_before
ExpectPy
--------
Homepage: http://expectpy.sourceforge.net/
Review based on version: 1.8.3
Release date: 2000-11-03
Author: Michael P. Reilly
Uses Don Libes' expect library: yes
Uses Tkinter: no
Sample usage:
spawn_id = ExpectPy.spawn('/bin/ed', 'ed', '/dev/null')
spawn_id.expect((ExpectPy.EXACT, "0\r\n", 0))
spawn_id.send('0a\rHello\r.\rw\r')
spawn_id.expect((ExpectPy.EXACT, '.\r\nw\r\n', 0))
rc = spawn_id.expect(
(ExpectPy.EXACT, '6\r\n', 1),
(ExpectPy.GLOB, '*\r\n', 2)
)
if rc == 1:
stdout.write('Test okay\n')
status = 1
elif rc == 2:
stdout.write('Test failed\n')
status = 0
Why this isn't good enough:
- there is no way to wait for input from more than one process at a
time.
- no support for expect_after and expect_before
- cumbersome syntax
- needs TCL
Pexpect
-------
Homepage: http://sourceforge.net/projects/pexpect/
Review based on version: 0.99
Release date: 2003-09-17
Author: Noah Spurrier
Uses Don Libes' expect library: no
Uses Tkinter: no
Sample usage:
child = pexpect.spawn('ssh -l %s %s'%(user, host))
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, 'password: '])
if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
sys.exit (1)
if i == 1: # SSH does not have the public key. Just accept it.
child.sendline ('yes')
child.expect ('password: ')
child.sendline(password)
Why this isn't good enough:
- there is no way to wait for input from more than one process at a
time.
- no support for expect_after and expect_before
However, this seems to have better pty support than any of the other
expect modules. It also have a screenscaping module that is probably
very useful if you want to write a program that interacts with a
curses-style program. If you only need to talk to one process at a
time, this seems to be a fairly mature and complete expect-like
module.
This is the only module that seems to be actively maintained.