Python tar file write tar to pipe
I want to create a tar file and pipe it to http upload.
However, it seems that the python tarfile is doing a search, making it impossible to connect to the next process.
Here is the code
tar = tarfile.open('named_pipe', mode='w')
tar.add('file1')
p.close()
'named_pipe' is a named pipe file created by the mkfifo command when I run it and cat named_pipe in another terminal I got the error
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
Any ideas?
+3
source to share
1 answer
We do something similar, but on the other hand. We have a web application that is passing a tarfile to the visitors browser, which means we are passing it over an http session. The trick is to pass the open and writeable file descriptor to the tarfile.open () call, along with the mode set to "w |". Something like that:
# 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)
+3
source to share