【发布时间】:2013-01-14 10:24:43
【问题描述】:
我正在使用以下脚本通过 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")
我不知道为什么,但删除 "\r" 会破坏此代码。那么"\r" 在这个脚本中做了什么,我一般什么时候使用"\r"?
注意:我知道“回车”,但仍然无法确定它在我的脚本中使用。我在 Linux 中运行这个脚本。
【问题讨论】:
-
这是一个Carriage return
-
我用谷歌阅读了关于回车的所有内容,但仍然无法弄清楚它在我上面的脚本中的用途。
-
我建议你也看看:docs.python.org/release/2.2.3/ref/strings.html 它解释了所有转义字符
-
这个问题可能也被问到了,因为只打印一个 \r 字符并没有做什么,这是提前行。