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

yipeiwu_com5年前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里面小数点精度的控制

要求较小的精度 round()内置方法 这个是使用最多的,刚看了round()的使用解释,也不是很容易懂。round()不是简单的四舍五入的处理方式。 For the built-in...

Python实现获取照片拍摄日期并重命名的方法

Python实现获取照片拍摄日期并重命名的方法

本文实例讲述了Python实现获取照片拍摄日期并重命名的方法。分享给大家供大家参考,具体如下: python获取照片的拍摄日期并重命名。不支持重复处理的中断。 重命名为:拍摄日期__原文...

python文件比较示例分享

复制代码 代码如下:# 比较两个字符串,如果不同返回第一个不相同的位置# 如果相同返回0def cmpstr(str1, str2):    col = 0...

基于Django的python验证码(实例讲解)

基于Django的python验证码(实例讲解)

验证码 在用户注册、登录页面,为了防止暴力请求,可以加入验证码功能,如果验证码错误,则不需要继续处理,可以减轻一些服务器的压力 使用验证码也是一种有效的防止crsf的方法 验证码效果如下...

Python3.2中Print函数用法实例详解

本文实例讲述了Python3.2中Print函数用法。分享给大家供大家参考。具体分析如下: 1. 输出字符串 >>> strHello = 'Hello World...