You can perfirm this task in few ways but I would recommend using regular expression. First of all, telnet code should be modified a little bit:
def read_until(cue, timeout=2):
return tn.read_until(cue, timeout)
telnetReturnedValue = read_until('200 OK: //MYHOUSE/254')
Example code which would extract value of "level":
import re
rePattern = 'level=(\d+)'
matchTuple = re.search(rePattern,telnetReturnedValue)
if(matchTuple!=None):
levelValue = matchTuple.groups()[0]
print(levelValue)
Re library documentation: http://docs.python.org/2/library/re.html .
UPDATE: Answering more detailed whittie83's question, there are many ways to write this program. It is still about Python basics so I would recommend learning from at least some tutorial, for example: http://docs.python.org/2/tutorial/.
Anyway, I would put regexp code into function like this:
import re
def parseTelnetOutput(output,rePattern='level=(\d+)'):
print(output)
matchTuple = re.search(rePattern,output)
if(matchTuple!=None):
someValue = matchTuple.groups()[0]
return(someValue)
#telnet functions definitions and telnet initiation
levelsList = [] #empty table for extracted levels
#some telnet commands...
tn_write('project load MYHOUSE\r\n')
print(read_until('200 OK.'))#not parsing, only printing
tn_write('net open 254\r\n')
parseResult = parseTelnetOutput(read_until('200 OK: //MYHOUSE/254/56/1'))
levelsList.append(parseResult) # adding extracted level into a table
tn_write('net open 254\r\n')
parseResult = parseTelnetOutput(read_until('200 OK: //MYHOUSE/254/56/2'))
levelsList.append(parseResult) # adding extracted level into a table
#etc...
for pr in levelsList: #iterating list of extracted levels
print(pr)
It is only a suggestion. You can merge 3 functions into 1, embed variables and functions into a class and so on. It is up to you.