Python "\r" 在下面的脚本中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14606799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:57:23  来源:igfitidea点击:

What does "\r" do in the following script?

pythontelnet

提问by Alinwndrld

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" from above code make the script non-working. So what does "\r" do in this script and when to use "\r" in general?

我不知道为什么但是从上面的代码中删除“\r”会使脚本不起作用。那么“\r”在这个脚本中做了什么以及一般什么时候使用“\r”?

Note: I know about "Carriage Return" but still could not figure out its use in my script. I am running this script in Linux.

注意:我知道“回车”,但仍然无法弄清楚它在我的脚本中的用途。我在 Linux 中运行这个脚本。

采纳答案by Some programmer dude

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. 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).

  1. ...

  2. 在 Net-ASCII 中,CR 不得出现,除非紧跟在 NUL 或 LF 之后,后者 (CR LF) 指定“换行”功能。今天,正如上面所指定的,CR 通常应该只在后面跟着 LF 时出现。因为页面布局最好用其他方式完成,因为 NUL 在某些编程语言中有特殊的解释,并且为了避免其他类型的混淆,最好避免使用 CR NUL 如上所述。

  3. LF CR 不应出现,除非作为多个 CR LF 序列的副作用(例如,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.

因此 Telnet 中的换行符应该始终是,'\r\n'但大多数实现要么没有更新,要么保留旧的'\n\r'以向后兼容。

回答by NPE

\ris 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);
  • LF (\n);
  • CR (\r).
  • CR+LF ( \r\n);
  • LF ( \n);
  • CR ( \r)

The \n\r(LF+CR) looks unconventional.

\n\r(LF + CR)看起来标新立异。

edit:My reading of the Telnet RFCsuggests that:

编辑:我对Telnet RFC 的阅读表明:

  1. CR+LF is the standard newline sequence used by the telnet protocol.
  2. LF+CR is an acceptable substitute:
  1. CR+LF 是 telnet 协议使用的标准换行符序列。
  2. 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”)。

回答by isedev

Actually, this has nothing to do with the usual Windows / Unix \r\nvs \nissue. The TELNET procotol itself defines \r\nas the end-of-line sequence, independently of the operating system. See RFC854.

实际上,这与通常的 Windows / Unix \r\nvs\n问题无关。TELNET 协议本身定义\r\n为行尾序列,与操作系统无关。请参阅RFC854

回答by Inbar Rose

'\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'如果我想在输出中写一些东西,而不是换行并写其他东西,我想写一些我想写的东西已经写过,这是 linux 或 windows 命令行中有多少程序能够在同一行上更改“进度”信息。

nowadays most systems use only the '\n'to denote a newline. but some systems use both together.

现在大多数系统只使用'\n'来表示换行符。但有些系统同时使用两者。

you can see examples of this given in some of the other answers, but the most common are:

你可以在其他一些答案中看到这样的例子,但最常见的是:

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

and some other programs also have specific uses for them.

其他一些程序也有特定的用途。

for more information about the history of these characters

有关这些角色历史的更多信息