Lin*_*inf 5 python pipe tarfile
我想创建一个 tar 文件并将其通过管道传输到 http 上传。
然而,似乎 python tarfile 模块执行查找,这使得无法通过管道传输到下一个进程。
这是代码
tar = tarfile.open('named_pipe', mode='w')
tar.add('file1')
p.close()
Run Code Online (Sandbox Code Playgroud)
'named_pipe' 是由 mkfifo 命令创建的命名管道文件,当我运行它并在另一个终端中 cat 命名管道时,出现错误
tar = tarfile.open('named_pipe', mode='w')
File "/usr/lib/python2.7/tarfile.py", line 1695, in open
return cls.taropen(name, mode, fileobj, **kwargs)
File "/usr/lib/python2.7/tarfile.py", line 1705, in taropen
return cls(name, mode, fileobj, **kwargs)
File "/usr/lib/python2.7/tarfile.py", line 1566, in __init__
self.offset = self.fileobj.tell()
IOError: [Errno 29] Illegal seek
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我们做了类似的事情,但从另一个角度来看。我们有一个 Web 应用程序,可以将 tar 文件提供给访问者浏览器,这意味着我们通过 http 会话流式传输它。技巧是将打开且可写的文件句柄传递给 tarfile.open() 调用,并将模式设置为“w|”。像这样的东西:
# assume stream is set to your pipe
with tarfile.open(name="upload.tar",
mode = "w|",
fileobj = stream,
encoding = 'utf-8') as out:
out.add(file_to_add_to_tar)
Run Code Online (Sandbox Code Playgroud)