Skip to content

Instantly share code, notes, and snippets.

@0xsha
Created November 4, 2019 10:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xsha/3c297e3c179835e1da8eb9196bc5e956 to your computer and use it in GitHub Desktop.
Save 0xsha/3c297e3c179835e1da8eb9196bc5e956 to your computer and use it in GitHub Desktop.
# dirbuster Asyncio example
# © 0xSha.io
# 11/2019
import asyncio
from aiohttp import ClientSession
import time
# global
lst = []
async def fetch(url, session, queue):
    while True:
        # Get a "work item" out of the queue.
        x = await queue.get()
        print(x.strip())
        # Notify the queue that the "work item" has been processed.
        queue.task_done()
        async with session.get(url+x.strip()) as response:
            if response.status == 200:
                print(url+x.strip() + "----" + str(response.status))
            x = await response.read()
        lst.append(x)
with open("fuzz.txt", "r") as file:
   #https://github.com/Bo0oM/fuzz.txt/blob/master/fuzz.txt
    x = file.readlines()
url = "http://url.com/"
async def run(r):
    tasks = []
    start = time.time()
    queue = asyncio.Queue()
    for _ in range(len(x)):
        # await queue.put(x[_])
        queue.put_nowait(x[_])
    async with ClientSession() as session:
 # how many tasks?
        for i in range(r):
            task = asyncio.create_task(fetch(url, session, queue))
            tasks.append(task)
 #join them
        await queue.join()
 #cancel remaining
        [task.cancel() for task in tasks]
        await asyncio.gather(*tasks, return_exceptions=True)
        # await asyncio.gather(*asyncio.all_tasks(), ).cancel()
        # https://bugs.python.org/issue29432
        # you now have all response bodies in this variable
        # print(result)
        end = time.time()
        print(end - start)
if __name__ == '__main__':
    asyncio.run(run(500)) #change the number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment