开发者

pexpect timeout is not being used, only the default of 30 is being used

开发者 https://www.devze.com 2023-01-09 03:43 出处:网络
I\'m trying to do a lengthy operation but pexpect with the timeout argument doesn\'t seem to change the length of time before the timeout exception gets fired. Here is my code:

I'm trying to do a lengthy operation but pexpect with the timeout argument doesn't seem to change the length of time before the timeout exception gets fired. Here is my code:

child = pexpect.spawn('scp file user@:开发者_JAVA技巧/temp', timeout=300)

whichMatched = child.expect(['(?i)Password','Are you sure you want to continue connecting (yes/no)?'], timeout=300)

The exception shows that the timeout=30, which is the default.

after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 6222
child_fd: 4
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


It appears to work if you only specify the timeout in the .spawn call, you cannot override, or use timeout=300 in the .expect call by itself.


Just tried the following and it seems to work:

child.timeout=300
child.expect("...")


I believe @darricks is incorrect. Here's an example that shows that the timeout argument to expect() is respected even when not specifying timeout for spawn().

test_pexpect.py

#! /usr/bin/env python

import pexpect
child = pexpect.spawn('sleep 50')
i = child.expect(['.* password:', 'Are you sure you want to continue connecting'], timeout=40)

Here's output from Linux. The output from test_pexpect.py lists "timeout: 30". That's just showing the timeout for spawn(). But the output from time, at the bottom, shows the script terminated at 40 seconds. So the expect() timeout was respected.

$ time test_pexpect.py
Traceback (most recent call last):
  File "./test_pexpect.py", line 5, in <module>
    i = child.expect(['.* password:', 'Are you sure you want to continue connecting'], timeout=40)
  File "/usr/rx30/musl/Python-2.7.14.install/lib/python2.7/site-packages/pexpect/spawnbase.py", line 341, in expect
    timeout, searchwindowsize, async_)
  File "/usr/rx30/musl/Python-2.7.14.install/lib/python2.7/site-packages/pexpect/spawnbase.py", line 369, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/rx30/musl/Python-2.7.14.install/lib/python2.7/site-packages/pexpect/expect.py", line 119, in expect_loop
    return self.timeout(e)
  File "/usr/rx30/musl/Python-2.7.14.install/lib/python2.7/site-packages/pexpect/expect.py", line 82, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0xf76b30ac>
command: /bin/sleep
args: ['/bin/sleep', '50']
buffer (last 100 chars): ''

after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 31968
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile('.* password:')
    1: re.compile('Are you sure you want to continue connecting')

real    0m40.235s
user    0m0.053s
sys     0m0.043s
$
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号