[Ilugc] [TIP] Python script of 23 lines to download file using http

  • From: shakthimaan@xxxxxxxxx (Shakthi Kannan)
  • Date: Fri, 18 Nov 2011 11:09:33 +0530

Hi,

--- On Fri, Nov 18, 2011 at 7:47 AM, Girish Venkatachalam
<girishvenkatachalam at gmail.com> wrote:
| Partly due to the fact that we use httplib module
\--

Another example with urllib2:

=== BEGIN ===

#!/usr/bin/python

from optparse import OptionParser
import urllib2

parser = OptionParser()

parser.add_option("-u", "--url",
                  action="store", type="string", dest="url",
                  help="http download url")

(options, args) = parser.parse_args()

if options.url:
    u = urllib2.urlopen(options.url)
    local_file = open('/tmp/result.out', 'w')
    local_file.write(u.read())
    local_file.close()

=== END ===

Feel free to customize it to suit your needs.

SK

-- 
Shakthi Kannan
http://www.shakthimaan.com

Other related posts: