Picture upload package two kinds of third-party libraries function calls

Requests Import, OS 
# a function package Photo
DEF upload_pic (URL, file_path, token):
file_name = os.path.basename (file_path)

Files = {
'File': (file_name, file name #
open (file_path, 'rb ') # file path

)
}
headers = {
"the Authorization": "{} Bearer." the format (token)
}
R & lt requests.post = (URL, files = files, headers = headers)
return r.text




Photo # package two function
Import Requests

from requests_toolbelt.multipart.encoder Import MultipartEncoder
Import OS

DEF upload_img (URL, token, file_path):
# output file path name of the last file
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

Guess you like

Origin www.cnblogs.com/fyangq/p/12463927.html