Python 织物2.x:通过管道清除焦油和焦油
我要执行与此shell脚本等效的操作:Python 织物2.x:通过管道清除焦油和焦油,python,ssh,tar,invoke,fabric,Python,Ssh,Tar,Invoke,Fabric,我要执行与此shell脚本等效的操作: ssh visarend.solasistim.net tar -c /home/amoe/episodes | tar -vx - 但是使用Fabric 2.x。这是我的尝试,但我不确定问题出在哪里 remote_path = "/home/amoe/episodes" c = fabric.Connection('visarend.solasistim.net') with subprocess.Popen(['tar', '-vx'], std
ssh visarend.solasistim.net tar -c /home/amoe/episodes | tar -vx -
但是使用Fabric 2.x。这是我的尝试,但我不确定问题出在哪里
remote_path = "/home/amoe/episodes"
c = fabric.Connection('visarend.solasistim.net')
with subprocess.Popen(['tar', '-vx'], stdin=subprocess.PIPE) as reader_proc:
c.run(
"tar -c %s" % (remote_path,),
out_stream=reader_proc.stdin
)
这给了我一个错误:
File "/usr/local/lib/python3.5/dist-packages/invoke/runners.py", line 525, in write_our_output
stream.write(encode_output(string, self.encoding))
TypeError: a bytes-like object is required, not 'str'
还有一些。我理解这可能是因为我从
reader\u proc.stdin
获取的流是字节流,而不是unicode流。但是我不明白为什么需要unicode流,或者正确的更改是什么使其工作。我无法评论为什么通过fabric.Connection.run()
执行的任务被假定为生成文本流,但是,由于拉丁-1
编码的存在,打包到文本流对象中的实际二进制流可以在不失真的情况下重新解释为二进制流:
import fabric
import subprocess
from io import TextIOWrapper
remote_path = "/home/amoe/episodes"
c = fabric.Connection('visarend.solasistim.net')
with subprocess.Popen(['tar', '-vx'], stdin=subprocess.PIPE) as reader_proc:
c.run(
"tar -c %s" % (remote_path,),
out_stream=TextIOWrapper(reader_proc.stdin, encoding='latin-1'),
encoding='latin-1'
)
很不错的。。你能简单阐述一下使用
拉丁语-1
的意义吗?@cody事实上,任何字符映射编码(请参阅)都可以工作latin-1
(又称iso-8859-1)仅仅是最简单的编码(它将代码点0-255的连续范围映射到字节0x0-0xff)。