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
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!