Python的Telnetlib read_until'#'或'>',多发串的决心? [英] Python Telnetlib read_until '#' or '>', mutiple string determination?

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

问题描述

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

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

我只想 read_until()来检查哪些所需的字符串是第一位的,并做不同的动作。
或者是有任何等价的方法?

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

推荐答案

看的文档。阅读,直到希望的有望的字符串作为位置参数和一个可选的超时。我会做这样的:

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天全站免登陆