Python uses Baidu API to upload files to Baidu online storage for code sharing

Source: Internet
Author: User
This article mainly introduces Python's use of Baidu API to upload files to Baidu online storage for code sharing. This article uses a third-party library poster and provides links in the article, for more information about how to obtain access_token, see the OAuth 2.0 API opened by Baidu. We will not introduce it here.

Third-party Python library

Poster

The code is as follows:


# Coding: UTF-8
Import urllib
Import urllib2

_ Author _ = 'admin'
From poster. encode import multipart_encode
From poster. streaminghttp import register_openers

Register_openers ()

Def upload (fileName ):
"""
Upload files to Baidu Cloud through Baidu developer API
"""
Principal en, headers = multipart_encode ({"file": open ("E: \ PHPTest \ Test1 \ % s" % fileName, "rb ")})
Baseurl = "https://pcs.baidu.com/rest/2.0/pcs/file? "
Args = {
"Method": "upload ",
"Access_token": "0. a2834e35964a7b0704252wef160507c1.2592000.1386326697.1060338330-1668780 ",
"Path": "/apps/ResourceSharing/% s" % fileName
}
Encodeargs = urllib. urlencode (args)
Url = baseurl + encodeargs

Print (url)

Request = urllib2.Request (url, en, headers)
Result = urllib2.urlopen (request). read ()
Print (result)


Upload ("host.txt ")

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.