本文介绍了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
发布评论