Python中paramiko exec_command执行远程脚本时实时返回的编码问题如何解决?

使用 paramiko 完成 执行远程 shell,实时获取结果

参考了这个: https://www.v2ex.com/t/431535

完成了

def line_buffered(f):
    line_buf = ""
    while not f.channel.exit_status_ready():
        line_buf += f.read(1)
        if line_buf.endswith('\n'):
            yield line_buf
            line_buf = ''
stdin, stdout, stderr = ssh.exec_command(shell)
            print logfile
        logfile = "static/logs/{0}".format(logfile)
        print logfile
        myfile = open(logfile, "w")
        for l in line_buffered(stdout):
            print l.decode("utf-8")
            myfile.write('ddd')
            myfile.write(l.decode("utf-8"))
            myfile.flush()
        myfile.close()

可以非阻塞实时返回,但有个问题,我的 shell 脚本中带有中文,就回报错

[2018-09-05 18:22:34,452: WARNING/ForkPoolWorker-1] game_1003_reback_1536142952196.log
[2018-09-05 18:22:34,453: WARNING/ForkPoolWorker-1] static/logs/game_1003_reback_1536142952196.log
[2018-09-05 18:22:34,467: WARNING/ForkPoolWorker-1] 'utf8' codec can't decode byte 0xe5 in position 0: unexpected end of data
[2018-09-05 18:22:34,669: WARNING/ForkPoolWorker-1] task fail, reason: UnicodeDecodeError('utf8', '\xe5', 0, 1, 'unexpected end of data') is not JSON serializable
[2018-09-05 18:22:34,669: WARNING/ForkPoolWorker-1] task fail, reason: 2260f200-a29b-4fec-9117-e8eee5903100
[2018-09-05 18:22:34,669: ERROR/ForkPoolWorker-1] Task opsbase.views.task_ssh_cmd[2260f200-a29b-4fec-9117-e8eee5903100] raised unexpected: EncodeError(TypeError("UnicodeDecodeError('utf8', '\\xe5', 0, 1, 'unexpected end of data') is not JSON serializable",),)
Traceback (most recent call last):
  File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/celery/app/trace.py", line 442, in trace_task
    uuid, retval, task_request, publish_result,
  File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/celery/backends/base.py", line 146, in mark_as_done
    self.store_result(task_id, result, state, request=request)
  File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/celery/backends/base.py", line 322, in store_result
    request=request, **kwargs)
  File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/django_celery_results/backends/database.py", line 20, in _store_result
    content_type, content_encoding, result = self.encode_content(result)
  File "/Users/apple/OneDrive/Code_7zGame/Envs/tdops/lib/python2.7/site-packages/django_celery_results/backends/database.py", line 43, in encode_content
    content_type, content_encoding, content = self._encode(data)

我是 python2.7 的,该加 .decode("utf-8")都加了

如果是直接使用平常方式,中文就能正常打印

result = stdout.read(), stderr.read()
        for i in result:
            print 

这个编码要怎么破,不要说换 python3.。


Python中paramiko exec_command执行远程脚本时实时返回的编码问题如何解决?

1 回复

遇到paramiko的exec_command输出编码问题,通常是因为远程终端的locale设置与本地解码方式不匹配。核心解决方案是统一编码,或者直接处理原始字节流。

最可靠的方法是读取原始字节流,然后按需解码:

import paramiko
import sys

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='user', password='pass')

stdin, stdout, stderr = client.exec_command('python3 /path/to/script.py')

# 方法1:逐行读取并处理编码
for line in stdout:
    # 先按字节读取,再尝试解码
    try:
        # 尝试UTF-8,这是最常见的
        decoded_line = line.decode('utf-8').rstrip()
    except UnicodeDecodeError:
        # 如果UTF-8失败,尝试系统默认编码或latin-1
        decoded_line = line.decode(sys.getdefaultencoding(), errors='replace').rstrip()
    print(decoded_line)

# 方法2:使用makefile并指定编码(更简洁)
for line in stdout.makefile("r", encoding="utf-8", errors="replace"):
    print(line.rstrip())

client.close()

如果知道远程服务器的确切编码(比如通过locale charmap命令查看),可以直接指定:

# 如果远程是GBK编码
for line in stdout:
    print(line.decode('gbk').rstrip())

或者更健壮的做法,先获取远程编码:

# 获取远程编码
_, locale_out, _ = client.exec_command('echo $LANG')
remote_encoding = locale_out.read().decode().strip().split('.')[-1] or 'utf-8'

# 使用获取的编码
for line in stdout:
    print(line.decode(remote_encoding, errors='replace').rstrip())

总结:直接处理字节流并按需解码最稳妥。

回到顶部