tarfile : read from a socket?
MRAB
python at mrabarnett.plus.com
Thu Feb 11 08:16:53 EST 2016
On 2016-02-11 12:53, Ulli Horlacher wrote:
> Antoon Pardon <antoon.pardon at rece.vub.ac.be> wrote:
>
>> > (How) can I read a tar file from a (tcp) socket?
>> > I do not have a pathname but a socket object from socket.create_connection
>>
>> # First you construct a file object with makefile.
>>
>> fo = socket.makefile()
>>
>> # Then you use the fileobj argument with tarfile.open.
>>
>> tarfile.open(mode='r', fileobj = fo)
>
>
> I have:
>
> sock = socket.create_connection((server,port))
> bs = kB64
> taro = tarfile.open(fileobj=sock.makefile('w',kB64),mode='w')
>
>
>
> Traceback (most recent call last):
> (...)
> File "./fexit.py", line 1838, in sex_send
> taro = tarfile.open(fileobj=sock.makefile('w',kB64),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()
> AttributeError: '_fileobject' object has no attribute 'tell'
>
I suppose you could write your own class to wrap the socket and provide
the required methods.
More information about the Python-list
mailing list