相关疑难解决方法(0)

我如何修剪空白?

是否有一个Python函数可以从字符串中修剪空格(空格和制表符)?

示例:\t example string\texample string

python string whitespace trim strip

1035
推荐指数
5
解决办法
109万
查看次数

为什么 paramiko 返回 \r\n 作为换行符而不是 \n?

我正在使用一个名为“paramiko”的 python SSH 库。它的使用方式如下:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user_name, password=password)
chan = ssh_pool[host].get_transport().open_session()
chan.exec_command(SOME_COMMAND)
Run Code Online (Sandbox Code Playgroud)

因为我需要在sudo中执行一些命令,所以我需要在exec_command之前调用chan.get_pty()以确保有可用的 tty 。

我发现在调用 get_pty() 后,标准输出从远程服务器返回,使用 \r\n 作为换行符而不是 \n。

例如:

>>> chan = ssh.get_transport().open_session()
>>> chan.exec_command("echo hello world")
>>> chan.recv(1000)
b'hello world\n'
Run Code Online (Sandbox Code Playgroud)

调用 get_pty() 后:

>>> chan = ssh.get_transport().open_session()
>>> chan.get_pty()
>>> chan.exec_command("echo hello world")
>>> chan.recv(1000)
b'hello world\r\n'
Run Code Online (Sandbox Code Playgroud)

我做了一些网络搜索,但找不到任何连接到pty / tty / newline / \r\n 的内容

我担心什么会导致这种变化。

为什么 paramiko 返回 \r\n 作为换行符而不是 \n?

python openssh tty paramiko pty

5
推荐指数
1
解决办法
5048
查看次数

标签 统计

python ×2

openssh ×1

paramiko ×1

pty ×1

string ×1

strip ×1

trim ×1

tty ×1

whitespace ×1