0x00 代码重现
源码
#!/usr/bin/env python3
# coding:utf-8
from fabric import Connection
def main():
c = Connection("{}@{}".format(user, host), connect_kwargs={"password": passwd})
with c.cd('/tmp'):
cmd_info = c.run("sh test.sh")
输出
姝e湪閮ㄧ讲鏂扮殑鏈嶅姟绋嬪簭...
0x01 解决方案
修改源码,指定输出文本的编码方式为utf-8
#!/usr/bin/env python3
# coding:utf-8
from fabric import Connection
def main():
c = Connection("{}@{}".format(user, host), connect_kwargs={"password": passwd})
with c.cd('/tmp'):
cmd_info = c.run("sh test.sh", encoding='utf-8')
再次输出
正在部署新的服务程序...
0x02 问题原因
本质问题是中文使用了gbk,程序使用了utf-8没有对应上。
另外,fabric的run方法是invoke.runners.Runner.run的一个封装,所以对于文本的编码需要增加encoding参数来控制。