Although urllib.urlopen returns an obj-like file, I don’t find it possible to do what you want without writing your own - for example, it does not support seek , but supports next , read , etc. And since you are dealing with the flow only forward, you will have to handle the jumps forward, restoring until you reach a certain point and caching any kind of rollback.
IMHO - you cannot effectively skip part of the network I / O stream (if you want to use the last byte, you still need to get all the previous bytes to get there - how you manage this storage is up to you).
I will be tempted to urlretrieve (or similar) the file and mmap as per your previous answer.
If your server can accept ranges (and the size of the response is also known from these derived blocks according to your example), then the possible work is to use http://en.wikipedia.org/wiki/Byte_serving (but I can’t say that I ever tried this).
Given an example, if you want only the first 16 and last 16 and don’t want to do something “too fantastic”:
from string import ascii_lowercase from random import choice from StringIO import StringIO buf = ''.join(choice(ascii_lowercase) for _ in range(50)) print buf sio_buf = StringIO(buf)
Output:
gpsgvqsbixtwyakpgefrhntldsjqlmfvyzwjoykhsapcmvjmar gpsgvqsbixtwyakp wjoykhsapcmvjmar
source share