Thursday, June 12, 2014

How To Get Remote File Size Without Downloading

Have you come across situations where you want to know the remote file size, but not actually downloading it? Sometimes your client / application cannot even tell how large is the remote file size.



curl

What you really want is the "Content-Length" value from the HTTP response when you request for the file. Curl can do just that for you:


$ curl -sI http://www.syntevo.com/download/smartgithg/smartgithg-macosx-6_0_2.dmg | grep Content-Length
Content-Length: 72661731

Is the number of bits difficult to understand? You can convert it to human-friendly size using numfmt from coreutils


$ curl -sI http://www.syntevo.com/download/smartgithg/smartgithg-macosx-6_0_2.dmg | grep Content-Length | sed 's/[^0-9]//g' | gnumfmt --to=iec
70M

You are piping the output to sed to remove all non-digits, and then use numfmt to convert to user-friendly string. Note that I use gnumfmt because GNU coreutils installed via Homebrew adds a 'g' prefix to prevent conflicts with OSX own commands. cut does not work because the string contains a \r suffix.


Python

 Another alternative is to use Python and httplib2


>>> import httplib2
>>> resp, content = httplib2.Http().request("http://www.syntevo.com/download/smartgithg/smartgithg-macosx-6_0_2.dmg")
>>> resp['content-length']
'72661731'


Remember to install the httplib2 module (pip install httplib2) or else you will see this error


ImportError: No module named 'httplib2'



Windows

One reason developers prefer OSX and Linux is because the environment is very conducive and natural for development work. In Windows, every extra functionality need to come from an application.

You are in luck for this task though, you can use PowerShell's Invoke-WebRequest. Good luck!




No comments:

Post a Comment