Skip to content

Instantly share code, notes, and snippets.

@markph0204
Created November 14, 2017 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markph0204/f9cf508547a574ec7fb13881b2657567 to your computer and use it in GitHub Desktop.
Save markph0204/f9cf508547a574ec7fb13881b2657567 to your computer and use it in GitHub Desktop.
import requests
from time import sleep
import os
import asyncio
import aiohttp
async def submit_and_wait():
print('Submitting request')
request_url = '<base url>/create_request'
headers = {'Authorization': '<auth code>'}
multiple_files = {'file1': open('images/test_image_1.jpg', 'rb'),
'file2': open('images/test_image_2.jpg', 'rb'),
'file3': open('images/test_image_3.jpg', 'rb'),
'file4': open('images/test_image_4.jpg', 'rb'),
'crop_data': open('crop.json', 'rb')}
# --- submit request
async with aiohttp.ClientSession() as session:
async with session.post(request_url, headers=headers, data=multiple_files) as resp:
data = await resp.json()
print(data)
print()
if resp.status == 200:
print('Request submitted')
url = os.path.join(data['folder_url'], 'video.mp4')
else:
print('Failed to successfully submit request: ' + str(resp.status))
return
# --- check URL
print('Checking url...')
while True:
print('Rechecking URL...')
status = await get_head_status(url)
print(status)
if status == 200:
print("URL valid, exiting... " + url)
break
await asyncio.sleep(5)
async def get_head_status(url):
async with aiohttp.ClientSession() as session:
async with session.head(url) as resp:
# print(await resp.text())
status = resp.status
return status
if __name__ == '__main__':
# async
tasks = []
for t in range(0, 10):
task = submit_and_wait()
tasks.append(task)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment