文件上传status 400_python requests文件上传/下载

158c03c115e5eb4fa47dced721cb9036.gif

点击关注,我们共同每天进步一点点!

文件上传

在做接口自动化的时候,有时需要上传文件,比如更改头像等等,在request里,通过files参数来上传

import requests

base_url = 'http://httpbin.org'
file = {'file': open(r'E:\00.jpg', 'rb')}
r = requests.post(base_url + '/post', files=file)

print(r.text)  

文件下载

第一种方式

import requests


def dowload_file(file_path):
    headers = {"Referer": "https://xx315.xx315.nex"}
    cookie = {"Cookie": "ASP.NET_SessionId=bij"}

    r = requests.get(url='https://xx315.xx315',
                     cookies=cookie,
                     headers=headers,
                     stream=True)

    if r.status_code == 200:
        with open(file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                f.write(chunk)


dowload_file('F:\\123.xlsx')

注意:

文件如果不存在,会在当前目录下生成一个文件,有内容会清空在写入

第二种方式

import requests
import shutil


def download_file_raw(file_path):
    url = 'https://xx315.xx315.net/Ashx/Export'
    cookie = {"Cookie": 'ASP.NET_SessionId=sjl8'}
    r = requests.get(url=url,
                     cookies=cookie,
                     stream=True
                     )

    if r.status_code == 200:
        with open(file_path, 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)


download_file_raw('F:\\123.xlsx')
原文地址: https://www.cnblogs.com/zouzou-busy/p/11407709.html f1c3567c341433dd08b06f9d40fa5f1f.png f5f18af379cc2750f7cda0d4323ebc56.gif

喜欢请关注,有用请转发~

升职、加薪、无漏测-点“在看”

17a7ec34e9b424533c8e01a2b65c4027.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值