Three ways to download files in Python

Source: Internet
Author: User
Tags ftp site http authentication

In Python development, the most common way to download files is to use the Urllib or URLLIB2 module via HTTP.

Of course you can also use Ftplib to download files from the FTP site. In addition, Python provides another way to requests.

Here's a look at three ways to download a zip file:
Method One:

" Downloading with urllib "  'http://***/test/demo.zip'"downloading with urllib  "" Demo.zip ")

Method Two:

" Downloading with urllib2 "  'http://***/test/demo.zip'== f.read () with open ( " Demo2.zip " " WB "  as code:code.write (data)


Method Three:

" downloading with requests "  'http://***/test/demo.zip'= requests. Get (URL) with open ("demo3.zip" "WB" )  as code:code.write (r.content)

It seems that using urllib is the simplest, one-sentence statement. Of course you can abbreviate urllib2 into:

f = urllib2.urlopen (URL) with open ("demo2.zip""WB "  as Code:code.write (F.read ())

================================================

Python requests recently studied this research can add me QQ29295842

Using Python in HTTP-related processing is an unnecessary hassle, including the URLLIB2 module's ability to get comprehensive functionality at a tremendous cost of complexity. The requests module, compared to the Urllib2,kenneth Reitz, is more concise and supports a complete simple use case.



A simple example:
Imagine we're trying to get a resource from http://example.test/using the Get method and look at the return code, Content-type header information, and response's main content. It's easy to do this, either with URLLIB2 or requests.
Urllib2



[Python] View plaincopy





>>> Import Urllib2

>>> url = ' http://example.test/'

>>> response = urllib2.urlopen (URL)

>>> Response.getcode ()

200

>>> response.headers.getheader (' Content-type ')

' Text/html; Charset=utf-8 '

>>> Response.read ()

' Hello, world! '


Requests



[Plain] View plaincopy





>>> Import Requests

>>> url = ' http://example.test/'

>>> response = requests.get (URL)

>>> Response.status_code

200

>>> response.headers[' Content-type ']

' Text/html; Charset=utf-8 '

>>> response.content

U ' Hello, world! '



The two methods are similar, and requests uses the property name to obtain the corresponding property value relative to the URLLIB2 call method that reads the property information in the response.
There are two subtle but important differences between the two:

1 requests automatic return message with Unicode decoding

2 requests automatically saves the returned content, so you can read it multiple times, unlike Urllib2.urlopen (), which returns only one object that resembles a file type that can only be read once.



The 2nd is the annoying thing about manipulating code in a python interactive environment

A more complicated example: Now let's try the example of a complex point: using the Get method to get Http://foo.test/secret resources, this time requires basic HTTP authentication. Using the code above as a template, it seems like we just need to change the code between Urllib2.urlopen () to Requests.get () to a request to send Username,password.

This is the method of Urllib2:



[Python] View plaincopy





>>> Import Urllib2

>>> url = ' Http://example.test/secret '

>>> Password_manager = urllib2. Httppasswordmgrwithdefaultrealm ()

>>> password_manager.add_password (None, URL, ' Dan ', ' H0tdish ')

>>> Auth_handler = urllib2. Httpbasicauthhandler (Password_manager)

>>> opener = Urllib2.build_opener (Auth_handler)

>>> Urllib2.install_opener (opener)

>>> response = urllib2.urlopen (URL)

>>> Response.getcode ()

200

>>> Response.read ()

' Welcome to the secret page! '



A simple method that instantiates 2 classes, then builds a third class, and finally loads the global URLLIB2 module, and finally calls the Urlopen, what are the two complex classes?


Are you confused, all URLLIB2 documents here http://docs.python.org/release/2.7/library/urllib2.html

How does the requests solve the same problem?


Requests



[Plain] View plaincopy





>>> Import Requests

>>> url = ' Http://example.test/secret '

>>> response = requests.get (URL, auth= (' Dan ', ' H0tdish '))

>>> Response.status_code

200

>>> response.content

U ' Welcome to the secret page! '



Just add a auth keyword function when calling the method
I bet you can remember it without checking the documentation.


Error handling errors handlingrequests The handling of errors is also very important. If you use an incorrect user name and password, URLLIB2 will throw a urllib2. Urlerror error, however requests will return a normal response object as you would expect. Just look at the Boolean value of Response.ok to know if the login was successful.



[Python] View plaincopy





>>> response = requests.get (URL, auth= (' Dan ', ' Wrongpass '))

>>> Response.ok

False



Some of the other features:
* Requests is as simple as the API for head, POST, PUT, PATCH, and Delete methods
* It can handle multipart uploads and also supports automatic transcoding
* Better Documentation
* There are more

Requests is good, you can try it next time you need to use HTTP.


Three ways to download files in Python

Related Article

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.