python+requests——高级用法——上传文件

import requests

url = 'http://httpbin.org/post'

wj = {'file':open('C:\\Users\\del\\Desktop\\新建文件夹\\1.jpg','rb')}

resp = requests.post(url,files = wj)

print(resp.text)
print(type(open('C:\\Users\\del\\Desktop\\新建文件夹\\1.jpg','rb')))   #返回结果:<class '_io.BufferedReader'>

上传文件只需要指定post方法的files参数即可,files参数的值可以是BufferedReader对象,该对象可以用python语言的内置函数open返回。


定义要上传的文件,字段中必须有一个key为file的值,值类型是BufferedReader,可以用open函数返回。


post方法将上次的文件转换为base64编码形式。

猜你喜欢

转载自www.cnblogs.com/xiaobaibailongma/p/12355192.html