Sending a file to strapi using python

requirements.txt

Pillow==10.1.0
python-dotenv==1.0.0
requests==2.31.0

main.py

# Python version 3.12.0
# You have to install requirements.txt 
# pip install -r requirements.txt
# Set ENV variables API_URL && API_SECRET
# API_SECRET is a custom strapi token (you can create from admin panel)
# API_URL === http://localhost:1337

from dotenv import load_dotenv
from PIL import Image
from pathlib import Path
import requests

def convert_image_to_webp(image_path: Path) -> Path:
    # Convert image to webp and save file
    image = Image.open(image_path)
    image = image.convert("RGB")
    image.save(image_path.with_suffix(".webp"), "webp", quality=92, optimize=True)
    return image_path.with_suffix(".webp")

def upload_image(image_path: Path) -> None:
    res = requests.post(
        os.getenv("API_URL") + "/api/upload",
        files={"files": (image_path.name, open(image_path, "rb"), "image/webp")},
        headers={
            "Authorization": f"Bearer {os.getenv('API_SECRET')}",
        },
    )

I am using this. I hope also helps to you