python使用requests实现发送带文件请求功能

作者:qq_492448446 时间:2023-11-03 14:23:13 

1. requests发送文件功能

Requests 使得上传多部分编码文件变得很简单

url = 'http://httpbin.org/post'
files = {'file': open('D:/APPs.png', 'rb')}
r = requests.post(url, files=files)
print(r.text)

你可以显式地设置文件名,文件类型和请求头:

url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
print(r.text)

如果你想,你也可以发送作为文件来接收的字符串:

url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

r = requests.post(url, files=files)
print(r.text)

如果你发送一个非常大的文件作为 multipart/form-data 请求,你可能希望将请求做成数据流。默认下 requests 不支持, 但有个第三方包 requests-toolbelt 是支持的。你可以阅读 toolbelt 文档 来了解使用方法。

2. requests发送多个文件请求

只要把文件设到一个元组的列表中,其中元组结构为 (form_field_name, file_info)
按照如下格式发送数据

data = {'ts_id': tsid}
files = [('images',('1.png', open('/home/1.png', 'rb'),'image/png')),('images',('2.png', open('/home/2.png', 'rb'),'image/png'))]
r = requests.post(url, data=data, files=files)
print r.text

3. Django 接收文件

附带介绍Django里面如何接收图片文件数据:
读取文件:

from werkzeug.utils import secure_filename

def upload_file(request):
   if request.method == 'POST':
       uploaded_files = request.FILES.getlist("images")
       try:
           for file in uploaded_files:
               filename = secure_filename(file.name)
               handle_uploaded_file(os.path.join(ft, filename), file)
       except Exception as e:
           result_json = {"msg": str(e)}
       result = {
           'json': result_json
       }
       return JsonResponse(result, safe=False)

保存文件:

def handle_uploaded_file(filename, f):
   try:
       destination = open(filename, 'wb+')
       for chunk in f.chunks():
           destination.write(chunk)
       destination.close()
   except Exception as e:
       raise Exception('save %s failed: %s' % (filename, str(e)))

requests 官网:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#post-multipart-encoded

来源:https://blog.csdn.net/qq_30273575/article/details/128182059

标签:python,requests,发送,请求
0
投稿

猜你喜欢

  • php+ajax+h5实现图片上传功能

    2024-05-22 10:05:39
  • 仿china.nba.com焦点图轮播展示效果(ie6,ff)

    2008-04-21 12:54:00
  • 高考要来啦!用Python爬取历年高考数据并分析

    2021-06-10 04:08:25
  • python 中的 super详解

    2023-09-07 01:27:35
  • python基础之元组

    2021-06-28 08:06:24
  • python开发之文件操作用法实例

    2022-08-07 02:03:45
  • asp如何显示最后十名来访者信息?

    2010-06-09 18:45:00
  • Ext2.0.2经典的一个JS组件(带EXT中文手册)

    2009-04-13 12:24:00
  • SQL Server数据类型char、nchar、varchar、nvarchar的区别浅析

    2024-01-14 06:57:15
  • js 代码优化点滴记录

    2024-05-28 15:41:47
  • Python 中如何使用 virtualenv 管理虚拟环境

    2022-02-20 00:57:44
  • Python模拟登录12306的方法

    2023-03-11 09:12:36
  • python自定义类并使用的方法

    2022-08-16 14:36:29
  • vue使用pdf.js预览pdf文件的方法

    2024-04-27 16:06:24
  • Python读取Word文档中的Excel嵌入文件的方法详解

    2022-12-21 14:11:23
  • Python实现字符串的逆序 C++字符串逆序算法

    2022-04-10 01:35:54
  • Python 第一步 hello world

    2021-03-25 06:42:15
  • MySql中的常用参数查询

    2024-01-18 06:00:30
  • 带你深入了解MySQL语句优化的基本原则

    2008-11-27 17:00:00
  • javascript中typeof操作符和constucor属性检测

    2024-05-09 10:37:27
  • asp之家 网络编程 m.aspxhome.com