Python Telnetlib读到'#';或'&燃气轮机';,多重字符串确定?

Python Telnetlib读到'#';或'&燃气轮机';,多重字符串确定?,python,port,automated-tests,telnet,telnetlib,Python,Port,Automated Tests,Telnet,Telnetlib,或 我只想让read_until()检查哪个字符串先出现,然后执行不同的操作。 或者有任何等效的方法吗?请查看。Read Till希望所需字符串作为位置参数和可选超时。我会这样做: if (tn.read_until() == '>'): action1 else: action2 如果需要多个不同的字符,可以使用read\u some() 更好地使用: >>> while True: #really you should set some sort o

我只想让
read_until()
检查哪个字符串先出现,然后执行不同的操作。 或者有任何等效的方法吗?

请查看。Read Till希望所需字符串作为位置参数和可选超时。我会这样做:

if (tn.read_until() == '>'):
    action1
else:
    action2
如果需要多个不同的字符,可以使用
read\u 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
这将返回一个包含三个项的元组,第一个(索引为0)是匹配的列表项的索引,第二个是和对象匹配的,第三个是输出文本

您可以使用buff_tpl[2]打印缓冲区输出,并使用buff_tpl[0]知道哪个项匹配

>>> 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
>>> 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
buff_tpl = tn.expect(["#", ">"])