Python use Baidu API upload file to Baidu Network disk code sharing _python

Source: Internet
Author: User

about how to get access_token this can own check Baidu Open OAuth 2.0 API. There is no introduction here.

Third party Python Library

Poster

Copy Code code as follows:

# Coding:utf-8
Import Urllib
Import Urllib2

__author__ = ' Administrator '
From Poster.encode import Multipart_encode
From poster.streaminghttp import register_openers

Register_openers ()

def upload (fileName):
"""
Through Baidu developer API upload file to Baidu Cloud
"""
DataGen, 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.a2834e35964a7b0704242wef160507c1.2592000.1386326697.1060338330-1668780",
"Path": "/apps/resourcesharing/%s"%filename
}
Encodeargs = Urllib.urlencode (args)
url = baseurl + Encodeargs

Print (URL)

Request = Urllib2. Request (URL, DataGen, headers)
result = Urllib2.urlopen (request). Read ()
Print (Result)


Upload ("Host.txt")

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.