4

我想创建一个 tar 文件并将其通过管道传输到 http 上传。

但是,似乎 python tarfile 模块执行了查找,这使得无法通过管道传输到下一个进程。

这是代码

tar = tarfile.open('named_pipe', mode='w')
tar.add('file1')
p.close()

'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

有任何想法吗?

4

1 回答 1

4

我们做类似的事情,但从另一边。我们有一个网络应用程序,它向访问者浏览器提供一个 tarfile,这意味着我们通过 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)
于 2014-08-15T19:49:16.203 回答