Created
April 5, 2022 05:13
-
-
Save socketbox/b63d7507ab7353715d990e1cf7b7482c to your computer and use it in GitHub Desktop.
Using async IO to test rate limiting
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
from requests_threads import AsyncSession | |
import requests | |
import multiprocessing | |
import sys | |
req_count = 500 | |
async_sess = AsyncSession(n=req_count) | |
async def make_sign_in_call(cookie: dict): | |
status = 200 | |
count = 0 | |
for _ in range(req_count): | |
⦙ resp = await async_sess.get("https://somedomain.org/en/auth/#/signin", | |
⦙ ⦙ ⦙ ⦙ ⦙ cookies=cookie) | |
⦙ status = resp.status_code | |
⦙ print(status) | |
⦙ print(count) | |
⦙ count+=1 | |
print("Last status: {}.".format(status)) | |
return | |
def get_csrf_cookie(url: str): | |
""" | |
takes a url | |
returns a session with csrf cookie set | |
""" | |
sess = requests.Session() | |
resp = sess.put(url, allow_redirects=True) | |
if resp.status_code != 200: | |
⦙ print("Error: {}".format(resp.status_code)) | |
⦙ sys.exit() | |
print("Session cookies: {}".format(sess.cookies)) | |
return sess | |
async def _main(): | |
cookie_url = "https://somedomain.org/api/auth/session/current" | |
sess = get_csrf_cookie(cookie_url) | |
csrf_guid = sess.cookies.get("kolibri_csrftoken") | |
assert len(csrf_guid) == 64 | |
req_cookie = {"kolibri_csrf": csrf_guid} | |
await make_sign_in_call(req_cookie) | |
if __name__ == '__main__': | |
async_sess.run(_main) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment