首页   注册   登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python 学习手册
Python Cookbook
Python 基础教程
Python Sites
PyPI - Python Package Index
http://www.simple-is-better.com/
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
V2EX  ›  Python

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

  •  
  •   fanne · 168 天前 · 968 次点击
    这是一个创建于 168 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    目前尚无回复
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   1044 人在线   最高记录 4385   ·  
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 14ms · UTC 18:23 · PVG 02:23 · LAX 10:23 · JFK 13:23
    ♥ Do have faith in what you're doing.
    沪ICP备16043287号-1