使用 requests
库发送文件
files = {'upload_file': open('file.txt','rb')}
r = requests.post(url, files=files)
requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file. The filename will be included in the mime header for the specific field
- mime header 中的文件名自动设置为
file.txt
也可以自己设置文件名,例如filex
:files = {'upload_file': ('filex', open('file.txt','rb'))}
还可以设置文件内容
files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}
发送文件的时候发送其它参数
files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}
r = requests.post(url, files=files, data=values)
来源:https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests