Timeout exceeded in read_nonblocking

在写pxssh登录远程主机的代码,遇到pexpect.TIMEOUT: Timeout exceeded in read_nonblocking(),依靠万能google找到答案
有同学也遇到相同问题,但都没有解决http://bbs.chinaunix.net/thread-1673357-1-1.html
看到一同学说(http://tech.foolpig.com/2010/01/08/pexpect/)加两行代码能解决问题,果然解决。
self.sendline()
time.sleep(0.5)

那是为什么呢?

I don’t know if anyone ever got back to you with a fix, but for me the fix was really simple.

I just added a self.sendline() and a time.sleep(0.5) right before the first read_nonblocking() call within synch_original_prompt

The reason is that you have to have stuff in the buffer in order to read it. In the case that they programmed for originally,**something** exists in their prompt (probably a space) after the $ or #. In my case (and probably yours) the last thing on the prompt **is** the $ or #, causing the buffer to be non-existent when the first read_nonblocking() happens. So all we do is put something there before the read.

Why not just get rid of the first read since it is just “clearing the buffer” anyway? Because then, although it would now work for us, it would no longer work for the original programmers.

– Jinno


One Response to “Timeout exceeded in read_nonblocking”

  1. stamhe说道:

    pexpect.spawn(cmd, “”, timeout=86400)
    done

Post a Comment