tarfile : read from a socket?
Peter Otten
__peter__ at web.de
Thu Feb 11 12:18:55 EST 2016
Ulli Horlacher wrote:
> Ulli Horlacher <framstag at rus.uni-stuttgart.de> wrote:
>
>> With
>>
>> taro = tarfile.open(fileobj=sock.makefile('w',kB64),mode='w|')
>>
>> I get no more error.
>
> Of course, this is the writing client.
>
> Now I have a small problem with the reading client.
>
> This code works so far:
>
> sfo = sock.makefile('r')
> taro = tarfile.open(fileobj=sfo,mode='r|')
> taro.extractall(path=edir)
>
> But it does not writes anything to the terminal to inform the user.
>
> When I use:
>
> for member in taro.getmembers():
> print('extracting "%s"' % member.name)
> taro.extract(member)
>
> I get the error:
>
> File "/usr/lib/python2.7/tarfile.py", line 556, in seek
> raise StreamError("seeking backwards is not allowed")
>
> Of course, a stream is not seekable.
>
> Any ideas?
A look into the source is often helpful ;)
$ cat extract_from_stream.py
import sys
from tarfile import TarFile
class MyTarFile(TarFile):
def extract(self, member, path="."):
print "extracting", member
return TarFile.extract(self, member, path)
tf = MyTarFile.open(fileobj=sys.stdin, mode="r|")
tf.extractall()
$ touch foo bar
$ tar -cf archive.tar foo bar
$ python extract_from_stream.py < archive.tar
extracting <TarInfo 'foo' at 0x7f2b3f394890>
extracting <TarInfo 'bar' at 0x7f2b3f3a4bd0>
More information about the Python-list
mailing list