import requests,os
# 封装上传图片函数一
def upload_pic(url,file_path,token):
file_name = os.path.basename(file_path)
files = {
'file': (file_name, # 文件名称
open(file_path, 'rb')# 文件路径
)
}
headers = {
"Authorization": "Bearer {}".format(token)
}
r = requests.post(url, files=files, headers=headers)
return r.text
# 封装的上传图片的函数二
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import os
def upload_img(url,token,file_path):
# 输出文件路径中最后文件名称
file_name=os.path.basename(file_path)
print(file_name)
m = MultipartEncoder({'image': (file_name, open(file_path, 'rb'), 'image/jpeg'), "accessToken": token})
headers = {'Content-Type': m.content_type,
"Authorization": "Bearer {}".format(token)}
result = requests.post(url, data=m, headers=headers, verify=False)
return result.status_code