pip install requests-toolbelt
from requests_toolbelt import MultipartEncoder import requests m = MultipartEncoder( fields={'field0': 'value', 'field1': 'value', 'field2': ('filename', open('file.py', 'rb'), 'text/plain')} ) r = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type})
参数重复的时候,就不用字典了,用list类型:
from requests_toolbelt import MultipartEncoder import requests m = MultipartEncoder( fields = [ ('source', ('f1.ext', f1, 'application/x-example-mimetype'), ('source', ('f2.ext', f2, 'application/x-example-mimetype'), ] ) r = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type})