get remote file size through http
Couple of days ago I wrote this code.
The requirement is to get the size of a remote file (http). For example: the Python program needs to find the size of the file, http://abc.com/dir/file1.mp3
Now here is a very stupid solution for this:
The stupidity happens in this line:
The requirement is to get the size of a remote file (http). For example: the Python program needs to find the size of the file, http://abc.com/dir/file1.mp3
Now here is a very stupid solution for this:
import urllib2
url = 'http://abc.com/dir/file1.mp3'
usock = urllib2.urlopen(url)
data = usock.read()
size = data.__len__() # size in bytes
size = size / 1024.0 # in KB (Kilo Bytes)
size = size / 1024.0 # size in MB (Mega Bytes)
...
The stupidity happens in this line:
data = usock.read()
where the whole file is being read to get it's size! This solution came to my mind first. But soon I understood that the file size can be found from the http response header. Here is a much better solution:
import urllib2
url = 'http://abc.com/dir/file1.mp3'
usock = urllib2.urlopen(url)
size = usock.info().get('Content-Length')
if size is None:
size = 0
size = float(size) # in bytes
size = size / 1024.0 # in KB (Kilo Bytes)
size = size / 1024.0 # size in MB (Mega Bytes)
...
Comments
import httplib
conn=httplib.HTTPConnection("www.abc.com")
conn.request("HEAD", "/dir/file1.mp3")
res=conn.getresponse()
fileSize=res.getheader('content-length')
#or res.getheaders() for all headers
conn.close()