Send Files via POST from Python Scripts
Utilizing Python scripts to send files through POST requests can be achieved effortlessly using the Requests library. This library provides a simple and effective method for uploading Multipart-encoded files.
<code class="python">with open('report.xls', 'rb') as f: r = requests.post('http://httpbin.org/post', files={'report.xls': f})</code>
This single line of code uploads the file, as demonstrated by the following response:
{ "origin": "179.13.100.4", "files": { "report.xls": "<censored...binary...data>" }, "form": {}, "url": "http://httpbin.org/post", "args": {}, "headers": { "Content-Length": "3196", "Accept-Encoding": "identity, deflate, compress, gzip", "Accept": "*/*", "User-Agent": "python-requests/0.8.0", "Host": "httpbin.org:80", "Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1" }, "data": "" }
This response confirms that the file was sent successfully. Utilizing the Requests library's straightforward functionality streamlines the process of sending files via POST requests from Python scripts.
The above is the detailed content of How to Send Files via POST Requests from Python Scripts?. For more information, please follow other related articles on the PHP Chinese website!