Search code examples
pythonstringparsingtelnet

Parsing string returned by Telnet.Read_Very_Eager line by line


In python, how do I parse this into strings? I expect the output to print each line, lines are found by a newline (\n) delimeter, but all I get are individual characters, for example, if the Server sends "This is a string this is another one" I get

"T h i s ..." And so on.

from time import sleep
tn = Telnet('myhost',port)
sleep(0.5)
response = tn.read_very_eager()

#How do I do something like this? I tried parsing it using string.split,
#all I got was individual characters.
foreach (line in response):
    print line, "This is a new line"

tn.close()

foreach (line in response):
    print line, "This is a new line"



Solution

  • IF I got your question correctly, it should look like this:

    from time import sleep
    
    tn = Telnet('myhost', port)
    sleep(0.5)
    
    response = tn.read_very_eager()
    
    for line in response.split():
        # Python 3.x version print
        print(line)
    
        # Python 2.x version print
        # print line
    
    tn.close()
    

    UPD: Updated answer according to the comment from OP.