I've written a simple script to expand my skills with python - however I've noticed something very strange when using BytesIO.
Here's my working script:
import requests
url = "http://MY.FAKE.IP.ADDR/Uploader.php"
file = open("test.jpg","rb")
action = {"file" : file, "name" : "test.jpg"}
requests.post(url, files=action)
However, when I try to do the same thing with an image in the memory (for example, a screenshot), the webserver gets a blank filename?? Here's my code for this:
import requests
from PIL import ImageGrab
from io import BytesIO
url = "http://MY.FAKE.IP.ADDR/phUploader.php"
file_name = "test.jpg"
im = ImageGrab.grab()
output = BytesIO()
im.save(output, "JPEG")
file = output.getvalue()
action ={"file":file,"name":file_name}
requests.post(url,files=action)
What the file looks like on the webserver:

However the file once renamed - does contain my image as intended. I can't find anything on this, it's really confusing why this script doesn't work.
You're constructing your requests files argument wrong, check the official documentation on how to construct it.
In your case: action = {"file": (file_name, file)} should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With