Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from the command line using python

I am looking for a quick way to download a file via HTTP, using a python one-liner from the command line (similar to the functionality of wget or curl). The idea is to enable a quick copy/paste to download distutils on Windows.

I know of one solution (see my answer below). I'm interested in other solutions that consider the following:

  • Concise
  • Most "pythonic" solution
  • Compatible with both python2 and python3
  • Cross-platform
  • Can deal with large files efficiently
  • No dependencies (we're fetching distutils here, it's unlikely we'll have access to requests at this stage)
  • Correctly handles various HTTP headers such as Content-Disposition
like image 296
dwurf Avatar asked Jun 18 '13 07:06

dwurf


1 Answers

The simplest solution I could come up with would be:

try:
    from urllib.request import urlretrieve
except ImportError:
    from urllib import urlretrieve

urlretrieve('http://example.org', 'outfile.dat')

urlretrieve takes care of downloading the resource to a local file and can deal with large files.

It however ignores Content-Disposition headers, if you want that to be considered, you'd need to use urlopen and parse the response headers yourself. Content-Disposition isn't a HTTP standard header, so i doubt you will find much support for it in the python http libraries...

like image 120
mata Avatar answered Oct 06 '22 08:10

mata