报各种编码出错的问题

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position 52: invalid continuation byte

我自己的情况是在putty中使用命令行执行python脚本或者执行PHP脚本去调用python脚本都是正常的,但是以http的方式访问php脚本去执行python脚本时就不对了。

在排除是python脚本本身编码问题后(PHP和python脚本都是用的utf-8编码),经过多次尝试,结果发现是系统环境编码的问题。

服务器环境:阿里云 Centos 7.6     PHP7.3   python3.6

最开始查看locale是:

LANG=en_US.UTF-8

所以尝试更换成C.UTF-8:

export LANG=”C.UTF-8″

但是无效,看资料说是要重启系统。结果重启系统后发现又变回了en_US.UTF-8,跟本没切换成功。

猜测是语言包可能不存在,然后根据找到的资料依次执行:

# 1. 安装编码集
localedef -v -c -i en_US -f UTF_8 C.UTF-8

# 2. 修改i18n内容 
vim /etc/sysconfig/118n
# 写入如下内容
LANG="C.UTF-8"
LC_ALL="C.UTF-8"

# 3. 修改profile
vim /etc/profile
export LANG="C.UTF-8"
export LC_ALL="C.UTF-8"

# 4. source
source /etc/profile

# 5. 重启查看locale 
shutdown -r now
locale 

这时发现系统的编码成了C.UTF-8,然后访问网址调用python脚本,成功!

 

参考链接:https://blog.csdn.net/Dontla/article/details/127900914