Python HTTP Basic authentication + download file to local

Source: Internet
Author: User

Simple code example

<span style= "FONT-SIZE:18PX;" >import urllib2 from base64 import encodestring url = ' http://www.xxx.com/xxxx.csv ' user = ' aaa ' passwd = ' bbbbb ' req = Urllib2. Request (URL) basestr = encodestring ('%s:%s '% (USER,PASSWD)) [: -1] Req.add_header (' Authorization ', ' Basic%s '% basestr) f = Urllib2.urlopen (req) data = F.read () with open ("2015010100.csv", "W") as Code:        code.write (data) </span>

Description:

<span style= "FONT-SIZE:18PX;" >url = ' Http://www.xxx.com/xxxx.csv ' </span>
is the address of the file you want to download, if it is accessed in the browser, it will pop up a box to let you enter the account number, password.

The difference between having a basic certification is this sentence.

<span style= "FONT-SIZE:18PX;" >req.add_header (' Authorization ', ' Basic%s '% basestr) </span>
You need to add a user name and password to the request.


Finally, you can access it with Urllib2.open ().

Then open the file and save it.


Python HTTP Basic authentication + download file to local

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.