Python's basic encrypted HTTP download

Source: Internet
Author: User

Instance:


Import Urllib2

Import base64,logging


def httpdownload (Testmodule,var):

Testmodule = Testmodule + '. Zip '

url = var[' httpres ' + '/' + testmodule ####### #此url为下载路径

Request = Urllib2. Request (URL)

Downdir = '/home/elves/agent/module/'

File = Downdir + testmodule

base64string = base64.encodestring ('%s:%s '% (var[' httpresuser '],var[' Httprespass ')). replace (' \ n ', ') ###### The user name, password code, remove the last return

Request.add_header (' Authorization ', ' Basic%s '% base64string) # # # # # # # # #将用户名密码以报文头的形式加到url

Try

result = Urllib2.urlopen (Request)

Except Exception,e:

Err = ' Requesturl_err:%s '%e

Logging.error (ERR)

fp = open (file, ' WB ')

Fp.write (Result.read ()) ##### #将文件读取, writing

Fp.close ()

Return ' Download Success '


Python's basic encrypted HTTP download

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.