Python Telnetlib read_until'#'或'>',多个字符串确定?

use*_*539 3 python port automated-tests telnet telnetlib

if (tn.read_until('>')):
    action1
else:
    action2
Run Code Online (Sandbox Code Playgroud)

要么

if (tn.read_until() == '>'):
    action1
else:
    action2
Run Code Online (Sandbox Code Playgroud)

我只想read_until()检查哪个String首先出现,然后执行不同的操作.或者有没有相同的方法?

msv*_*kon 5

看看文档.读取直到想要预期的字符串作为位置参数和可选的超时.我会这样做:

>>> try:
...     response = tn.read_until(">", timeout=120) #or whatever timeout you choose.
... except EOFError as e:
...     print "Connection closed: %s" % e

>>> if ">" in response:
...    action1
... else:
...    action2
Run Code Online (Sandbox Code Playgroud)

如果您想要多个不同的角色,可以使用 read_some()

>>> while True: #really you should set some sort of a timeout here.
...    r = tn.read_some()
...    if any(x in r for x in ["#", ">"]):
...        break
Run Code Online (Sandbox Code Playgroud)