Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2228498
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2130
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-31 14:30
个人简介

自己慢慢积累。

文章分类

全部博文(287)

分类: Python/Ruby

2019-04-30 11:32:58

说明:
该方式适用于请求类型 multipart/form-data。
具体表现是请求头(header)中
  1. Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryaPJUIAvGfcSPVJIJ
1、安装requests-toolbelt

  1. pip install requests-toolbelt
2、具体使用如下:

  1. from requests_toolbelt import MultipartEncoder
  2. import requests
  # text/plain 需要根据实际情况替换,可能是:'image/png'
  # fields 里参数个数是根据实际来定义的,几个文件就定义几个field ,具体的名字要根据实际情况调整

  1. m = MultipartEncoder(
  2.     fields={'field0': 'value', 'field1': 'value',
  3.             'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
  4.     )

  5. r = requests.post('', data=m,
  6.                   headers={'Content-Type': m.content_type})
  7. # 当然 header 可以在自己定义好的header里加入'Content-Type': m.content_type


阅读(1587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~