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赋值操作方法分享

一、序列赋值: x,y,z = 1,2,3 我们可以看作:x = 1,y = 2,z = 3 二、链接赋值: x = y = 1print id(x)print id(y) 大家可以看下...

python在Windows8下获取本机ip地址的方法

本文实例讲述了python在Windows8下获取本机ip地址的方法。分享给大家供大家参考。具体实现方法如下: import socket hostname = socket.ge...

Python中内建函数的简单用法说明

Python提供了一个内联模块buildin,该模块定义了一些软件开发中经常用到的函数,利用这些函数可以实现数据类型的转换、数据的计算、序列的处理等。 buildin模块的内置函数: 1...

python中pylint使用方法(pylint代码检查)

一、Pylint 是什么 Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。 Pylint 是一个 Pyth...

Python+OpenCV实现旋转文本校正方式

Python+OpenCV实现旋转文本校正方式

假设我们有一幅图像,图像中的文本被旋转了一个未知的角度。为了对文字进行角度的校正,我们需要完成如下几个步骤: 1、检测出图中的文本范围 2、计算出文本被旋转的角度 3、将图像旋转特定的角...