Skip to content

Instantly share code, notes, and snippets.

@socketbox
Created April 5, 2022 05:13
Show Gist options
  • Save socketbox/b63d7507ab7353715d990e1cf7b7482c to your computer and use it in GitHub Desktop.
Save socketbox/b63d7507ab7353715d990e1cf7b7482c to your computer and use it in GitHub Desktop.
Using async IO to test rate limiting
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