What does “\r” do in the following script?

后端 未结 4 942
傲寒
傲寒 2020-11-28 11:09

I am using following script to reboot my router using Telnet:

#!/usr/bin/env python

import os
import telnetlib
from time import sleep

host = \"192.168.1.1\         


        
4条回答
  •  孤独总比滥情好
    2020-11-28 11:34

    The '\r' character is the carriage return, and the carriage return-newline pair is both needed for newline in a network virtual terminal session.


    From the old telnet specification (RFC 854) (page 11):

    The sequence "CR LF", as defined, will cause the NVT to be positioned at the left margin of the next print line (as would, for example, the sequence "LF CR").

    However, from the latest specification (RFC5198) (page 13):

    1. ...

    2. In Net-ASCII, CR MUST NOT appear except when immediately followed by either NUL or LF, with the latter (CR LF) designating the "new line" function. Today and as specified above, CR should generally appear only when followed by LF. Because page layout is better done in other ways, because NUL has a special interpretation in some programming languages, and to avoid other types of confusion, CR NUL should preferably be avoided as specified above.

    3. LF CR SHOULD NOT appear except as a side-effect of multiple CR LF sequences (e.g., CR LF CR LF).

    So newline in Telnet should always be '\r\n' but most implementations have either not been updated, or keeps the old '\n\r' for backwards compatibility.

提交回复
热议问题