It is necessary to send a .txt file to the server, how can this be implemented?
Closed due to the fact that the essence of the question is not clear to the participants of 0xdb , freim , iluxa1810 , LFC , Air March 13 at 3:59 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- onerequests.post - andreymal
|
1 answer
Sending a file via requests.post ( Multipart-encoded ):
files = {'file': open('report.xls', 'rb')} rs = requests.post(url, files=files) Now more .
For testing, we will send the current script file to https://httpbin.org/post
import requests url = 'https://httpbin.org/post' abs_file_name = __file__ The simplest example of sending:
files = {'file': open(abs_file_name, 'rb')} rs = requests.post(url, files=files) print(rs) print(rs.text) With manual file name:
files = {'file': ('my_file.py', open(abs_file_name, 'rb'))} rs = requests.post(url, files=files) print(rs) print(rs.text) Sending a string as a file. It’s convenient that you don’t need to write data to the file
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')} rs = requests.post(url, files=files) print(rs) print(rs.text) You can send bytes:
files = {'file': ('report.csv', b'some,data,to,send\nanother,row,to,send\n')} rs = requests.post(url, files=files) print(rs) print(rs.text) |