简体   繁体   English

"\\r" 在下面的脚本中做了什么?

[英]What does "\r" do in the following script?

I am using following script to reboot my router using Telnet:我正在使用以下脚本使用 Telnet 重新启动路由器:

#!/usr/bin/env python

import os
import telnetlib
from time import sleep

host = "192.168.1.1"
user = "USER"
password = "PASSWORD"
cmd = "system restart"

tn = telnetlib.Telnet(host)
sleep(1)

tn.read_until("Login: ")
tn.write(user + "\n\r")
sleep(1)

tn.read_until("Password: ")
tn.write(password + "\n\r")
sleep(1)

tn.write(cmd + "\n\r")

I don't know why but removing "\\r" breaks this code.我不知道为什么,但删除"\\r"会破坏这段代码。 So what does "\\r" do in this script and when do I use "\\r" in general?那么"\\r"在这个脚本中做了什么,我一般什么时候使用"\\r"

Note: I know about "Carriage Return" but still could not figure out it is used in my script.注意:我知道“回车”,但仍然无法弄清楚我的脚本中使用了它。 I am running this script in Linux.我在 Linux 中运行这个脚本。

The '\\r' character is the carriage return, and the carriage return-newline pair is both needed for newline in a network virtual terminal session. '\\r'字符是回车符,并且网络虚拟终端会话中的换行符都需要回车 - 换行符对。


From the old telnet specification (RFC 854) (page 11): 旧的telnet规范(RFC 854) (第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"). 如所定义的,序列“CR LF”将使NVT定位在下一个打印行的左边缘处(例如,序列“LF CR”)。

However, from the latest specification (RFC5198) (page 13): 但是,从最新规范(RFC5198) (第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. 在Net-ASCII中,除非紧接着是NUL或LF,否则CR必须不出现,后者(CR LF)指定“新行”功能。 Today and as specified above, CR should generally appear only when followed by LF. 今天,如上所述,CR通常只有在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. 因为页面布局在其他方面更好,因为NUL在某些编程语言中有特殊的解释,并且为了避免其他类型的混淆,最好应该如上所述避免使用CR NUL。

  3. LF CR SHOULD NOT appear except as a side-effect of multiple CR LF sequences (eg, CR LF CR LF). 除了作为多个CR LF序列的副作用(例如,CR LF CR LF)之外,LF CR不应出现。

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. 因此,Telnet中的换行符应始终为'\\r\\n'但大多数实现都未更新,或保留旧的'\\n\\r'以实现向后兼容。

\\r is the ASCII Carriage Return (CR) character. \\r是ASCII 回车 (CR)字符。

There are different newline conventions used by different operating systems. 不同的操作系统使用不同的换行符约定。 The most common ones are: 最常见的是:

  • CR+LF ( \\r\\n ); CR + LF( \\r\\n );
  • LF ( \\n ); LF( \\n );
  • CR ( \\r ). CR( \\r )。

The \\n\\r (LF+CR) looks unconventional. \\n\\r (LF + CR)看起来非常规。

edit: My reading of the Telnet RFC suggests that: 编辑:我阅读Telnet RFC表明:

  1. CR+LF is the standard newline sequence used by the telnet protocol. CR + LF是telnet协议使用的标准换行序列。
  2. LF+CR is an acceptable substitute: LF + CR是可接受的替代品:

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"). 如所定义的,序列“CR LF”将使NVT定位在下一个打印行的左边缘处(例如,序列“LF CR”)。

'\\r' means 'carriage return' and it is similar to '\\n' which means 'line break' or more commonly 'new line' '\\r'表示'回车',它类似于'\\n' ,表示'换行'或更常见'换行'

in the old days of typewriters, you would have to move the carriage that writes back to the start of the line, and move the line down in order to write onto the next line. 在打字机的旧时代,你必须移动写回线的开头的托架,然后向下移动线以便写入下一行。

in the modern computer era we still have this functionality for multiple reasons. 在现代计算机时代,我们仍然有这个功能有多种原因。 but mostly we use only '\\n' and automatically assume that we want to start writing from the start of the line, since it would not make much sense otherwise. 但大多数情况下我们只使用'\\n'并自动假设我们想从行的开头开始写,因为否则它没有多大意义。

however, there are some times when we want to use JUST the '\\r' and that would be if i want to write something to an output, and the instead of going down to a new line and writing something else, i want to write something over what i already wrote, this is how many programs in linux or in windows command line are able to have 'progress' information that changes on the same line. 然而,有些时候我们想要使用JUST '\\r' _ '\\r' ,如果我想写一些东西到输出,而不是去一个新的行并写下别的东西,我想写在我已经写过的东西上,这是linux或windows命令行中有多少程序能够在同一行上改变“进度”信息。

nowadays most systems use only the '\\n' to denote a newline. 现在大多数系统只使用'\\n'来表示换行符。 but some systems use both together. 但有些系统同时使用它们。

you can see examples of this given in some of the other answers, but the most common are: 你可以在其他一些答案中看到这个例子,但最常见的是:

  • windows ends lines with '\\r\\n' windows以'\\r\\n'结束行
  • mac ends lines with '\\r' mac以'\\r'结束行
  • unix/linux use '\\n' unix / linux使用'\\n'

and some other programs also have specific uses for them. 和其他一些程序也有特定的用途。

for more information about the history of these characters 有关这些角色历史的更多信息

it is the carriage return control character. 它是回车控制字符。 http://en.wikipedia.org/wiki/Carriage_return http://en.wikipedia.org/wiki/Carriage_return

Actually, this has nothing to do with the usual Windows / Unix \\r\\n vs \\n issue. 实际上,这与通常的Windows / Unix \\r\\n vs \\n问题无关。 The TELNET procotol itself defines \\r\\n as the end-of-line sequence, independently of the operating system. TELNET procotol本身将\\r\\n定义为行尾序列,与操作系统无关。 See RFC854 . 请参阅RFC854

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM