Last active
August 18, 2021 18:47
-
-
Save im-noob/c9d78449f025f503bfbbdabef2f42c96 to your computer and use it in GitHub Desktop.
Send 1 milion request in thread
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
with open('urls.txt','w') as f: | |
for i in range(1000_000): | |
f.write('https://example.com'+'\n') | |
from urllib.parse import urlparse | |
from threading import Thread | |
import http.client, sys | |
from queue import Queue | |
import http.client | |
thread_count = 1000 #32000 | |
concurrent = 1000000 | |
def doWork(): | |
while True: | |
url = q.get() | |
# print('Processign Queue',url) | |
status, url = getStatus(url) | |
doSomethingWithResult(status, url) | |
q.task_done() | |
def getStatus(ourl): | |
try: | |
# print('SendingRequest',ourl) | |
url = urlparse(ourl) | |
conn = http.client.HTTPConnection(url.netloc) | |
conn.request("HEAD", url.path) | |
res = conn.getresponse() | |
return res.status, ourl | |
except Exception as e: | |
print(e) | |
return "error", ourl | |
def doSomethingWithResult(status, url): | |
print(status, url) | |
q = Queue(concurrent * 2) | |
for i in range(thread_count): | |
# print('Starting Queue:',i) | |
t = Thread(target=doWork) | |
t.daemon = True | |
t.start() | |
try: | |
for url in open('urls.txt'): | |
q.put(url.strip()) | |
# print('Adding Data to Queue',url) | |
q.join() | |
# print('Joinign Queue') | |
except KeyboardInterrupt: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment