【问题标题】:Python pexpect: var.expect('string') not returning expected values when 'string' isn't foundPython pexpect:var.expect('string') 未找到“string”时不返回预期值
【发布时间】:2016-08-12 17:49:23
【问题描述】:

我正在使用以下 Python 代码。代码相当简单,pexpect 将echo testcase 发送到终端,然后监视终端是否出现testcase,然后通过设置bool_value=True 来验证它是否看到了回显(假设它看到了预期的输出)。

#!/usr/bin/python
import pexpect

print('Testing case where pexpect finds the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('testcase')
print bool_value
testcase.close()

print('Testing case where pexpect does not find the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = not testcase.expect('someothervalue')
print bool_value
testcase.close()

我希望看到的是,在第一个测试用例之后,bool_value 将打印True。在这种情况下确实如此,但只有在添加了设置 bool_value = not testcase.expect('testcase') 的奇怪技巧之后(将其设置为 expect() 返回的逆)。

在第二个测试用例中,testcase.expect() 没有看到someothervalue 的预期值,所以我希望它返回false。相反,它返回一个异常:

Traceback (most recent call last):
  File "./echotest.py", line 12, in <module>
    bool_value = not testcase.expect('someothervalue')
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
    timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1521, in expect_loop
    raise EOF(str(err) + '\n' + str(self))
pexpect.EOF: End Of File (EOF). Exception style platform.
<pexpect.spawn object at 0x7f7667a9db10>
version: 3.1
command: /bin/echo
args: ['/bin/echo', 'testcase']
searcher: <pexpect.searcher_re object at 0x7f7667a9d790>
buffer (last 100 chars): ''
before (last 100 chars): 'testcase\r\n'
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 0
flag_eof: True
pid: 4619
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

我真的不确定如何根据文档告诉我的内容来纠正这种行为。每个 this previous implementation 添加 testcase.expect(pexpect.EOF()) 会返回相同的错误。

如何正确实现这个功能?

【问题讨论】:

  • 可能是因为echo 默认添加了换行符?尝试使用echo -n 或使用expect('testcase\n')。
  • 我将建议更改为 echo -n。抛出异常的唯一变化是before (last 100 chars): 'testcase\r\n' 行变为before (last 100 chars): 'testcase'。通过使用expect('testcase\n') 或expect('testcase\r\n') 或在代码中添加新的expect('\r\n') 行,异常不会改变。

标签: python python-2.7 pexpect


【解决方案1】:

事实证明,当系统没有输出pexpect 正在寻找的东西时,它会在不触发任何动作的情况下命中pexpect.EOF。因为我没有告诉它当它遇到 EOF 而没有看到它所期望的任何其他字符串时要做什么,所以它会返回一个异常。

这是我的代码,包含更正和测试用例。

#!/usr/bin/python
import pexpect

"""
Test for some known good value.
"""
print('Testing case where pexpect finds the expected value: ')
testcase = pexpect.spawn('echo testcase')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()

"""
Test for some known bad response
"""
print('Testing case where pexpect finds an expected bad value: ')
testcase = pexpect.spawn('echo badcase')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()

"""
If it gets no expected response at all, it will reach EOF. We have to define
some behavior for that case.
"""
print('Testing case where pexpect does not find any expected value: ')
testcase = pexpect.spawn('echo unknown')
bool_value = testcase.expect([pexpect.EOF,'testcase','badcase'])
if bool_value == 0:
    print('Unknown value presented to script.')
elif bool_value == 1:
    print('Success.')
elif bool_value == 2:
    print('Failed.')
print bool_value
testcase.close()

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2017-02-02
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多