结论: (作为使用py2.7的)你应该使用pexpect.spawn
而非: pexpect.spawnu
, 这是python3 的
当时报错如下
{19-01-12 19:10}pauldeMacBook-Pro:~/code/openssl_tar_test paul% ./pe_test.py
Traceback (most recent call last):
File "./pe_test.py", line 11, in <module>
child.expect('(?i)password')
File "/Library/Python/2.7/site-packages/pexpect/spawnbase.py", line 339, in expect
compiled_pattern_list = self.compile_pattern_list(pattern)
File "/Library/Python/2.7/site-packages/pexpect/spawnbase.py", line 237, in compile_pattern_list
self._pattern_type_err(p)
File "/Library/Python/2.7/site-packages/pexpect/spawnbase.py", line 188, in _pattern_type_err
for ast in self.allowed_string_types])
TypeError: got <type 'str'> ('(?i)password') as pattern, must be one of: <type 'unicode'>, pexpect.EOF, pexpect.TIMEOUT
我在这里找到答案
https://github.com/pexpect/pexpect/blob/master/examples/ftp.py
# Note that, for Python 3 compatibility reasons, we are using spawnu and
# importing unicode_literals (above). spawnu accepts Unicode input and
# unicode_literals makes all string literals in this script Unicode by default.
child = pexpect.spawnu('ftp ftp.openbsd.org')