I want to read a file on internet from a particular offset in python.
Like in normal file handler (return by open()) we have a seek() api.
Is there any way of doing this when reading from network.
import urllib.request
g = urllib.request.urlopen('http://tools.ietf.org/rfc/rfc2822.txt')
g.seek(20)
f=g.read(100)
print(f)
I tried the following but it obviosuly gave error
io.UnsupportedOperation: seek
What can I do to solve this?
You can use Range
header (only if the server supports it):
import urllib.request
req = urllib.request.Request('http://tools.ietf.org/rfc/rfc2822.txt',
headers={'Range': 'bytes=20-'})
g = urllib.request.urlopen(req)
f = g.read(100)
print(f)
But not all server support Range
. You should check response header. If server does not support it, you should skip bytes by read them.
import urllib.request
req = urllib.request.Request('http://tools.ietf.org/rfc/rfc2822.txt',
headers={'Range': 'bytes=20-'})
g = urllib.request.urlopen(req)
if 'Content-Range' not in g.info(): # <-----
# OR if g.status != http.client.PARTIAL_CONTENT
g.read(20) # <-----
f = g.read(100)
print(f)