Created
November 14, 2017 13:29
-
-
Save markph0204/f9cf508547a574ec7fb13881b2657567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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