Python Telnetlib read_until'#'或'>',是否确定多个字符串? [英] Python Telnetlib read_until '#' or '>', multiple string determination?

查看:409
本文介绍了Python Telnetlib read_until'#'或'>',是否确定多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (tn.read_until('>')):
    action1
else:
    action2

if (tn.read_until() == '>'):
    action1
else:
    action2

我只希望read_until()检查哪个所需的String首先出现,并执行不同的操作. 还是有任何等效的方法?

I just want the read_until() to check which desired String comes first, and do different actions. Or is there any equivalent ways?

推荐答案

查看文档.读取直到希望 expected 字符串作为位置参数和可选的超时值.我会这样:

Look at the docs. Read until wants the expected string as a positional argument and an optional timeout. I would do it like this:

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

如果需要多个不同的字符,可以使用read_some()

If you want multiple different characters you can use 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

这篇关于Python Telnetlib read_until'#'或'>',是否确定多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆