Python的Telnetlib read

编程入门 行业动态 更新时间:2024-10-23 10:21:39
本文介绍了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

本文发布于:2023-08-07 13:52:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319405.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   Telnetlib   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!