Python summarizes the methods for downloading files under the HTTP protocol,

Source: Internet
Author: User

Python summarizes the methods for downloading files under the HTTP protocol,

This article describes several common methods for downloading python files, including htttplib2 and urllib.

1. Simple File Download

Use htttplib2 with the following code:

h = httplib2.Http()      url = 'http://www.bkjia.com/ip.zip'  resp, content = h.request(url)        if resp['status'] == '200':    with open(filename, 'wb') as f:      f.write(content)  

Use urllib with the following code:

filename = urllib.unquote(url).decode('utf8').split('/')[-1]  urllib.urlretrieve(url, filename) 

2. Large file downloads

def down_file():    url = "http://www.bkjia.com/download.abc"      file_name = url.split('/')[-1]    u = urllib2.urlopen(url)    f = open(file_name, 'wb')    meta = u.info()    file_size = int(meta.getheaders("Content-Length")[0])        file_size_dl = 0    block_sz = 8192    while True:      buffer = u.read(block_sz)      if not buffer:        break          file_size_dl += len(buffer)      f.write(buffer)    f.close()  

You can parse the url when obtaining the downloaded file name. The Code is as follows:

scheme, netloc, path, query, fragment = urlparse.urlsplit(url)  filename = os.path.basename(path)  if not filename:    filename = 'downloaded.file'  

3. resumable download
When using HTTP for download, you only need to set the Range in the header to perform resumable download. Of course, the server must first support resumable download.

Example of resumable download using the Python urllib2 module:

#!/usr/bin/python # -*- coding: UTF-8 -* ''' Created on 2013-04-15 Created by RobinTang A demo for Resuming Transfer ''' import urllib2  req = urllib2.Request('http://www.python.org/') req.add_header('Range', 'bytes=0-20') # set the range, from 0byte to 19byte, 20bytes len res = urllib2.urlopen(req)  data = res.read()  print data print '---------' print 'len:%d'%len(data) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.