zeffir4ik

Untitled

Jan 22nd, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. async def post_image_to_api(
  2.         session: aiohttp.ClientSession,
  3.         file_path: Path
  4. ) -> tuple[Path, str]:
  5.     boundary = uuid.uuid4().hex
  6.     headers = {
  7.         "Content-Type": f"multipart/form-data; boundary={boundary}",
  8.     }
  9.  
  10.     with open(file_path, "rb") as image_file:
  11.         data = aiohttp.FormData()
  12.         data.add_field("file", image_file, filename=file_path.name)
  13.         await asyncio.sleep(60 / 100) # <- Задержка перед отправкой
  14.         async with session.post("http://localhost:8000/images", data=data) as resp:
  15.             json_response = await resp.json()
  16.             return file_path, json_response["id"]
Advertisement
Add Comment
Please, Sign In to add comment