paramiko exec_command 执行远程机器脚本实时返回编码问题

2018-09-05 18:26:26 +08:00
 fanne

使用 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.。

4877 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/486491

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX