input = [1,2,3,4]
# output = [24,12,8,6]
input = [5,3,2,4,1]
# output = [24,40,60,30,120]
length = len(input)
a = b = []
for index in range(length):
    r=1
    for i in input[:index]:
        r *= i
    a.append(r)
    r=1
    for i in input[length-index-1:]:
        r *= i
    b.append(r)
b.reverse()
b = b + [1]
_ = b.pop(0)
# print(a)
# print(b)
print(list(map(lambda x, y: x * y, a, b)))

这种算是知道就会,不知道也很难想出来的吧?

前提是网盘有这个文件,好处在于不用开浏览器...

# -*- coding: utf-8 -*-

import os
import sys
import zlib
import json
import hashlib
import requests
from argparse import ArgumentParser
# 此处的BDUSS是网页源代码中的XDUSS
cookies = {"BDUSS":"pansec_***"}
ua_1 = {
    "X-Requested-With":"XMLHttpRequest",
    "User-Agent":"netdisk;2.0.0.4;PC;PC-Windows;10.0.17763;uploadplugin",
    "Content-Type":"application/x-www-form-urlencoded",
    "Accept":"*/*",
    "Accept-Language":"zh-CN,zh;q=0.8",
    "Accept-Charset":"GBK,utf-8;q=0.7,*;q=0.3",
    }

def crc(path):
    prev = 0
    for line in open(path,"rb"):
        prev = zlib.crc32(line, prev)
    return prev & 0xFFFFFFFF
    
def slice_md5(path):
    with open(path, "rb") as f:
        bin_obj = f.read(256 * 1024)
        part_md5 = hashlib.new('md5', bin_obj).hexdigest()
    return part_md5
    
def info(path):
    crc32 = crc(path)
    part_md5 = slice_md5(path)
    size = fs = os.path.getsize(path)
    f = open(path, "rb")
    hash_obj = hashlib.md5()
    md5_list = []
    while (fs > 0):
        if fs < 4194304:
            bin_obj = f.read(fs)
        else:
            bin_obj = f.read(4194304)
        hash_obj.update(bin_obj)
        md5_list.append(hashlib.new('md5', bin_obj).hexdigest())
        fs -= 4194304
    f.close()
    md5 = hash_obj.hexdigest()
    print("crc32", crc32)
    print("前256K的md5", part_md5)
    print("分片md5", md5_list)
    print("文件md5", md5)
    return crc32, part_md5, md5_list, md5, size
    
def rapidupload(remote_fullpath, md5, part_md5, crc32, size):
    url = "http://pan.baidu.com/api/rapidupload"
    query = {
        "clienttype":"6",
        "version":"2.0.0.4",
        }
    data = {
        "path":remote_fullpath,
        "content-md5":md5,
        "slice-md5":part_md5,#前256K的md5值
        "content-crc32":crc32,#10进制
        "content-length":size,
    }
    # 返回中errno为404说明自己网盘没有这个文件
    rapid_res = requests.post(url, data=data, headers=ua_1, cookies=cookies, params=query)
    rapid_resd = json.loads(rapid_res.content.decode("utf-8"))
    print(rapid_resd)
    if rapid_resd.get("errno") in [-8, 0]:
        if rapid_resd.get("errno") == -8:
            print("已存在同名文件")
        if rapid_resd.get("errno") == 0:
            print(rapid_resd["info"])
        return True
    else:
        print(rapid_resd.get("errno"))
        return False
        
def main():
    # 需要安装argparse
    parser = ArgumentParser(description="尝试秒传文件至百度网盘")
    parser.add_argument("-fp", "--fpath",help="本地文件路径")
    parser.add_argument("-bp", "--bpath",help="网盘路径")
    args = parser.parse_args()
    _, name = os.path.split(args.fpath)
    remote_fullpath = args.bpath + "/" + name
    crc32, part_md5, md5_list, md5, size = info(args.fpath)
    flag = rapidupload(remote_fullpath, md5, part_md5, crc32, size)
    if flag:
        sys.exit("{} 已秒传 位于 {}".format(name, remote_fullpath))
    else:
        sys.exit("{} 秒传失败".format(name))
    
if __name__ == "__main__":
    main()

在控件安装情况下,文件以分片形式上传
请求如下:

POST /rest/2.0/pcs/file?method=upload&app_id=250528&type=tmpfile&dir=%2F/&filename=**X** HTTP/1.1
Cookie: BDUSS=**pansec_X**
Accept: text/*
Content-Type: multipart/form-data; boundary=----------KM7ei4Ef1gL6ae0ei4gL6GI3GI3gL6
User-Agent: netdisk;2.0.0.4;PC;PC-Windows;10.0.17763;uploadplugin
Connection: Keep-Alive
Pragma: no-cache
Host: c.pcs.baidu.com
Cache-Control: no-cache
Content-Length: **X**

------------KM7ei4Ef1gL6ae0ei4gL6GI3GI3gL6
Content-Disposition: form-data; name="Filename"

------------KM7ei4Ef1gL6ae0ei4gL6GI3GI3gL6
Content-Disposition: form-data; name="Filedata"; filename=""
Content-Type: application/octet-stream
**...binary...**

其中**包含起来的地方为个人信息和提交信息,其中:

  • filename为32位大写的字母与数字组合 暂不知产生方法,也许是随机的
  • BDUSS实际上这里和通常使用的值不一样,这个值通过查看网页源代码可以看到,为XDUSS值
  • Content-Length会自动决定
  • 通过multipart/form-data提交的二进制数据分片大小为4M
    返回如下:
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf8
Connection: keep-alive
x-bs-client-ip: **X**
x-bs-request-id: **X**
x-bs-file-size: 4194304
Content-MD5: **X**
x-bs-meta-crc32: **X**
x-poms-part-key: **X**
x-bs-version: **X**
Vary: Accept-Encoding
Server: POMS/CloudUI 1.0
Content-Length: **X**

{"md5":"**X**","request_id":**X**}

其中x-bs-client-ip和x-bs-request-id为base64编码,解码后内容形式如下:
*.*.*.* 为你的出口/公网ip
*.*.*.*:*:*:201*-0*-* *:*:* 为内网ip+port+request_id+时间
x-bs-file-size为分片大小,一般是4M
Content-MD5和即为分片的md5,和返回的值一致
x-bs-meta-crc32似乎与我在电脑上计算的CRC32值不一致
x-poms-part-key和x-bs-version一致 32位数字字母小写组合

分片上传返回的md5在全部分片上传结束之后,通过下面这个接口组合成完整的文件

POST /api/create?a=commit&norename&clienttype=6&version=2.0.0.4 HTTP/1.1
Cookie: BDUSS=*
Connection: keep-alive
X-Requested-With: XMLHttpRequest
User-Agent: netdisk;2.0.0.4;PC;PC-Windows;10.0.17763;uploadplugin
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Host: pan.baidu.com
Cache-Control: no-cache
Content-Length: 266

path=//*&isdir=0&size=13729324&block_list=["","","",""]&method=POST

其中path为网盘路径,根目录则是//+文件名 size是文件总大小,block_list包含一个list,依次是md5值。
最后是浏览器请求了这个,似乎是刷新作用

GET /statistics?clienttype=0&version=v5&op=webupload&type=uploadSuccess&uk=*&product=pan&from=NativeUploader&detailKey=success&filesize=文件大小&is_member=0&success=1&uploadStyle=0&averageSpeed=0&filePath=本地路径&diskPath=/&file={"name":"文件名","type":"文件后缀包括点","server_path":"网盘路径根目录为/","progress":0,"size":"文件大小","path":"本地路径","dir":0,"status":3,"index":2,"resume":true} 
HTTP/1.1
Host: update.pan.baidu.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: image/webp,image/*,*/*;q=0.8
Referer: https://pan.baidu.com/disk/home?
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: zh-CN,zh;q=0.8
Cookie: *