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
Barbara StreisandOriginal
2024-10-29 23:54:291091browse

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>

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!

Statement:
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