2016-08-12 2 views
0

다음 파이썬 코드로 작업하고 있습니다. 코드는 매우 간단하며 pexpect는 터미널에 echo testcase을 전송 한 다음 터미널에서 후속 모양 인 testcase을 모니터링 한 다음 bool_value=True (기대되는 출력이 있다고 가정)을 설정하여 에코를 보았는지 확인합니다. 내가 볼 것으로 예상 무엇Python pexpect : 'string'을 찾을 수 없을 때 var.expect ('string')가 예상 값을 반환하지 않습니다.

#!/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()가 반환하는 것과 반대의 값으로 설정).

두 번째 테스트 케이스에서 은 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 implementationtestcase.expect(pexpect.EOF())을 추가하면 같은 오류가 반환됩니다.

이 기능을 올바르게 구현하려면 어떻게해야합니까?

+2

아마도 'echo'가 기본적으로 개행 문자를 추가했기 때문일 수 있습니까? 'echo -n'을 사용하거나'expect ('testcase \ n')'를 사용하십시오. – Bakuriu

+0

나는'echo -n'에 제안 된 변경을했다. throw 된 예외의 유일한 변경 사항은'before (last 100 chars) : 'testcase \ r \ n'''가'before (last 100chars) :'testcase''가 된 것입니다. 예외는'expect ('testcase \ n')'또는'expect ('testcase \ r \ n')'또는 새로운'expect ('\ r \ n')'행을 코드에 추가함으로써 변경되지 않습니다. –

답변

0

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()