Python使用百度API上传文件到百度网盘代码分享

yipeiwu_com6年前Python基础

关于如何获取 access_token 这个可以自己查百度开放的OAuth 2.0 的 API。这里不做介绍。

第三方 Python 库

poster

复制代码 代码如下:

# 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):
    """
    通过百度开发者 API 上传文件到百度云
    """
    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")

相关文章

整理Python 常用string函数(收藏)

字符串中字符大小写的变换 1. str.lower() //小写 >>> 'SkatE'.lower() 'skate' 2. str.upper() //大写 >...

Python实现读取json文件到excel表

本文实例为大家分享了Python实现读取json文件到excel表,供大家参考,具体内容如下 一、需求 1、'score.json' 文件内容: { "1":["小花",99,1...

Python GUI布局尺寸适配方法

如下所示: #coding=utf-8 #布局自定义尺寸 from tkinter import * class App: def __init__(self,master...

python3下载抖音视频的完整代码

python3下载抖音视频的代码如下所示: # -*- coding:utf-8 -*- from contextlib import closing import request...

python实现排序算法解析

python实现排序算法解析

本文实例为大家分享了python实现排序算法的具体代码,供大家参考,具体内容如下 一、冒泡排序 def bububle_sort(alist): """冒泡排序(稳定|n^2m)...