python实现Content-Type:application/octet-stream

Content-Type:application/octet-stream

这种传输用于图片|文本传输:

我也是在操作河马云手机中,往云机传输图片和文本txt遇到的。
“”"=============================================================="""
接下来python实现下:
首先要先安装:requests-toolbelt

pip install requests-toolbelt

图片:

# -*- coding: utf-8 -*-
# @Author  : Codeooo
# @Time    : 2021/10/22



import random
from base64 import b64decode

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

def up(base64_img):
    url= 'https://hzsite.longene.com.cn/cardgroup!uploadChuckFile'
    headers={
        "Cookie":"JSESSIONID=1D70333FF886DDE6EDEA845EB8A7FDCA",
        # "Host":"hzsite.longene.com.cn",
        # "Origin":"https://hzsite.longene.com.cn",
        # "Pragma":"no-cache",
        # "Referer":"https://hzsite.longene.com.cn/js/webWorker_upload.js",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
    }

    multipart_encoder = MultipartEncoder(
        fields={
            "chuckSize":"1",
            "currentChuck":"1",
            "fileName": "1.png",
            # "Content-Type": "application/octet-stream",
            # "file": ("1.png", open('C:\\Users\Administrator\Desktop\index.png','rb'), 'application/octet-stream')
            "file": ("1.png",  b64decode(imgBase64(base64_img)), 'application/octet-stream')
        },
        # boundary = '-----------------------------' + "WebKitFormBoundary" + str(random.randint(1e28, 1e29 - 1))
        boundary = '----' + "WebKitFormBoundary" + generate_code()
    )

    headers['Content-Type'] = multipart_encoder.content_type
    print(multipart_encoder.content_type)

    result = requests.post(url, headers=headers, data=multipart_encoder)
    print(result.status_code)
    print(result.text)


def imgBase64(base64_img):
    return base64_img.replace("data:image/png;base64,","")



def generate_code(code_len = 16):
  all_char = '0123456789qazwsxedcrfvtgbyhnujmikolpQAZWSXEDCRFVTGBYHNUJIKOLP'
  code = ''
  for _ in range(code_len):
    num = random.randint(0,len(all_char)-1)
    code += all_char[num]
  return code


base64_img = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH0AAAB9AQAAAACn+1GIAAABxElEQVR4Xr2VsY20MBCFBzlwtjRgyW04c0tsA7vQALTkzG1YcgNL5sDy/M9wp9NF4z85hBD6JJiZx3sD8e+j0N+ARjRHImebViupIeB51+bl6VnswXkIOHUEe0ZU5J2GQVQc6zPa/wBB7S736yBAp84eJX/8T+sCIHq4vF/nt0ACgNINejhafP7+DCJIU0nvks+SN7ZjAI8a0mYK6aGHQCPeIJ5XW8xcruFE4Ori6UXppe3GfVoZ+DqxeQd6cqXbQTLgj1NbsKuG6t2nImDGrYEXwJrOI6BLrtMcDLk0xV5WBIwo6Lx6XjXfLxVBI7O4tHgUrzQGYIFd88mwD1S8/SGA1r1j3kgD17t1EUCPnXglDFcXuiUUAAZqjqH9FiFMd5AIONQ5phkBcvwZBMVyMQu0p3z2zMmgaWSOV1dfCHccAw7pUbvGMkh0tS6CaznVd2EO+atTGeQrENxc3kJXXQSQEJtm9eaJslcaRHDtD7tFqJ7blz8E0LcUoWZ96Lxf08oAu7DHzn60uq0tA9f1O6GKJ/K99RFwlDpzepA9buOOgEhT4bP0GA0Bz0eAtWniLvwQ6H9CbME0x7roXlYE/Pv4I/AP3E++ybG1pKEAAAAASUVORK5CYII='
up(base64_img)

文本:

# -*- coding: utf-8 -*-
# @Author  : Codeooo
# @Time    : 2021/10/22



import random
from base64 import b64decode

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

def up(base64_img,fileName):
    # url= 'https://ecsite.longene.com.cn/cardgroup!uploadChuckFile'
    url= 'https://hzsite.longene.com.cn/cardgroup!uploadChuckFile'

    headers={

        "Accept-Encoding": "gzip, deflate, br",
        "Cookie": "JSESSIONID=1D70333FF886DDE6EDEA845EB8A7FDCA",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
    }

    multipart_encoder = MultipartEncoder(
        fields={
            "chuckSize":"1",
            "currentChuck":"1",
            "fileName": f"{fileName}",
            "Content-Type": "application/octet-stream",
            "file": ("1.txt",  str.encode(txt), 'application/octet-stream')
        },
        boundary = '-----------------------------' + str(random.randint(1e28, 1e29 - 1))
    )

    headers['Content-Type'] = multipart_encoder.content_type

    result = requests.post(url, headers=headers, data=multipart_encoder)
    print(result.status_code)
    print(result.text)


def imgBase64(txt):
    return  str.encode(txt)


txt = '136xxxx7120----hpril64810'
fileName = '1.txt'
up(txt,fileName)


你可能感兴趣的:(Python爬虫,python)