美文网首页
ssh执行远程python脚本utf-8的问题,报Unicode

ssh执行远程python脚本utf-8的问题,报Unicode

作者: 风吹路过的云 | 来源:发表于2020-06-08 09:34 被阅读0次

需要ssh远程执行一个python脚本,在一个节点上是正常的,环境是centos7,bash。
在另一台节点上居然报错了,环境是centos7,zsh,报错信息如下:

ssh root@xx.xx.xx.xx "python3 /data/scripts/demo.py"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 144-150: ordinal not in range(128)

好明显是编码问题,检查了python文件的编码,文件头有coding utf-8,编码也是utf-8

#!/usr/bin/python3
# coding=utf-8

接着检查两个环境的env,locale,都是一样

[root@xx ~]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

那问题出在哪里,随后google之,有文章说要加PYTHONIOENCODING=utf-8,即执行命令如下:

ssh root@xx.xx.xx.xx "PYTHONIOENCODING=utf-8 python3 /data/scripts/demo.py"

问题解决!:)

相关链接
https://stackoverflow.com/questions/17374526/unicodeencodeerror-when-using-python-from-ssh

相关文章

网友评论

      本文标题:ssh执行远程python脚本utf-8的问题,报Unicode

      本文链接:https://www.haomeiwen.com/subject/hfaktktx.html