nova-api/api/netclient.py

62 lines
1.9 KiB
Python
Raw Normal View History

2023-07-25 02:42:53 +02:00
import os
2023-08-03 03:50:04 +02:00
import aiohttp
import asyncio
import aiohttp_socks
from dotenv import load_dotenv
2023-07-25 19:45:21 +02:00
import proxies
2023-07-25 02:42:53 +02:00
from helpers import exceptions
2023-07-25 02:42:53 +02:00
load_dotenv()
2023-08-03 03:50:04 +02:00
async def stream(request: dict, demo_mode: bool=False):
headers = {
'Content-Type': 'application/json'
}
for k, v in request.get('headers', {}).items():
headers[k] = v
for _ in range(3):
2023-08-03 03:50:04 +02:00
async with aiohttp.ClientSession(connector=proxies.default_proxy.connector) as session:
async with session.get(
# 'GET',
'https://checkip.amazonaws.com/'
) as response:
print(response.content)
print(type(response.content))
# html = await response.text()
# print(html)
# async with session.get(
# method='GET',
# url='https://checkip.amazonaws.com',
# method=request.get('method', 'POST'),
# url=request['url'],
# json=request.get('payload', {}),
# headers=headers,
# timeout=aiohttp.ClientTimeout(total=float(os.getenv('TRANSFER_TIMEOUT', '120'))),
# ) as response:
# try:
# await response.raise_for_status()
# except Exception as exc:
# if 'Too Many Requests' in str(exc):
# continue
# else:
# break
async for chunk in response.content.iter_chunks():
# chunk = f'{chunk.decode("utf8")}\n\n'
if demo_mode:
print(chunk)
yield chunk
2023-07-25 19:45:21 +02:00
if __name__ == '__main__':
2023-08-03 03:50:04 +02:00
asyncio.run(stream({'method': 'GET', 'url': 'https://checkip.amazonaws.com'}, True))