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编码形式。

 

posted @ 2020-02-23 23:52  小白龙白龙马  阅读(1532)  评论(0编辑  收藏  举报