Home > Backend Development > Python Tutorial > How to Send a File Using POST from a Python Script?

How to Send a File Using POST from a Python Script?

Barbara Streisand
Release: 2024-10-29 23:54:29
Original
1120 people have browsed it

How to Send a File Using POST from a Python Script?

How to Send a File Using POST from a Python Script

Sending a file using a POST request from a Python script is a straightforward process. With the help of the Requests library, you can easily accomplish this task.

Solution:

To send a file via a POST request, you can utilize the files parameter in the requests.post() function. Here's an example that demonstrates how to do this:

<code class="python">import requests

file_path = 'report.xls'  # Replace with your file's path
url = 'http://httpbin.org/post'  # Replace with your target URL

with open(file_path, 'rb') as file_handle:  # Open file in binary read mode
    response = requests.post(url, files={'report.xls': file_handle})

print(response.text)  # The response will contain details about the uploaded file</code>
Copy after login

By using this method, you can seamlessly send files as part of your POST requests from Python scripts.

The above is the detailed content of How to Send a File Using POST from a Python Script?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template