Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Telnetlib read_until '#' or '>', multiple string determination?

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

or

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

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

like image 822
user3352539 Avatar asked Dec 26 '22 12:12

user3352539


1 Answers

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

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
like image 86
msvalkon Avatar answered Feb 19 '23 10:02

msvalkon