gpt4 book ai didi

Python Telnetlib read_until '#' or '>' 多字符串判断?

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:54 31 4
gpt4 key购买 nike

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

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

我只希望 read_until() 检查哪个所需的字符串先出现,然后执行不同的操作。或者有什么等效的方法吗?

最佳答案

查看docs . Read until 需要预期的 字符串作为位置参数和可选的超时。我会这样做:

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

>>> 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 '#' or '>' 多字符串判断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074909/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com