How to Send Files via POST Requests from Python Scripts?

DDD
Release: 2024-10-28 05:09:02
Original
327 people have browsed it

How to Send Files via POST Requests from Python Scripts?

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>
Copy after login

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": ""
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!