Created
February 6, 2024 00:02
-
-
Save OlegKorn/415f8ddf53c1ef19ab6a426bd4a9a7ec to your computer and use it in GitHub Desktop.
last working version
This file contains hidden or 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 httpx | |
import asyncio | |
import aiohttp | |
import os | |
from fp.fp import FreeProxy | |
from time import sleep | |
import logging | |
THIS_DIR = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/') | |
def open_file(fr, to): | |
with open(f'{THIS_DIR}/ordered', "r", encoding='utf-8') as num_lines: | |
text = [line.strip() for line in num_lines.readlines()[fr:to]] | |
return text | |
def log(data): | |
FORMAT = '%(message)s' | |
logging.basicConfig( | |
handlers=[ | |
logging.FileHandler( | |
filename=f'{THIS_DIR}/test777', | |
encoding='utf-8' | |
) | |
], | |
level=logging.INFO, | |
format=FORMAT, | |
force=True # https://stackoverflow.com/questions/30861524/logging-basicconfig-not-creating-log-file-when-i-run-in-pycharm | |
) | |
logging.info(data.strip()) | |
async def fetch(session, url): | |
async with session.get(url) as response: #proxy=pr) as response: | |
return await response.text(), response.status | |
async def main(url): | |
semaphore = asyncio.Semaphore(3) | |
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session: | |
resp = await fetch(session, url) | |
print(url, resp[1]) | |
data = url + ' ' + str(resp[1]) | |
log(data) | |
for num in range(0, 2000, 100): | |
fr = num | |
to = num + 100 | |
urls = [i.strip() for i in open_file(fr, to)] | |
print(fr, to) | |
try: | |
loop = asyncio.get_event_loop() | |
future = [asyncio.ensure_future(main(url)) for url in urls] | |
loop.run_until_complete(asyncio.wait(future)) | |
print('Sleeping 2...') | |
sleep(2) | |
except Exception as e: | |
print(e) | |
break | |
# sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment