python 钉钉文件上传
from urllib3 import encode_multipart_formdataimport requestsheaders={}data={}url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'def post_files(url,headers,data,filename,filepath):
'''
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
'''
data['media']= ('test.py', open(r'C:\Users\Administrator\Desktop\test.py', 'rb').read())
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
r = requests.post(url, headers=headers, data=data)
print(json.loads(r.text)['media_id'])
from urllib3 import encode_multipart_formdataimport requestsheaders={}data={}url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'def post_files(url,headers,data,filename,filepath):
data['media']= ('test.py', open(r'C:\Users\Administrator\Desktop\test.py', 'rb').read())
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
r = requests.post(url, headers=headers, data=data)
print(json.loads(r.text)['media_id'])
赞0
踩0