[Python] AIOHTTP and ASYNCIO GRAPHQL request is replacing session_id in Cookie with another...

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    So I'm messing around with the leetcode GRAPHQL api and trying to hit up company endpoints, in the follow synchronous code that I wrote, it works perfectly and the cookie is also saved correctly. It returns all the data I need:

    def test_basic(query, slug):
    variables = {
    "skip": 0,
    "limit": 3,
    "favoriteSlug": slug,

    }

    try:
    request = requests.post("https://leetcode.com/graphql", json={'query': query, 'variables': variables}, headers=HEADERS, cookies=COOKIE)
    breakpoint()
    print(request.url)
    print(request.status_code)
    print(request.text)
    if request.status_code == 200:
    return request.json()

    except Exception as e:
    print(f"Error: {e}")
    return None


    However, I'm trying to get better at writing asynchronous code so I decided to load test these requests and make it more efficient. The problem here is that when I print the response, it has the altered cookie, however when I print the response.request_info() it is the correct cookies. I'm really confused as to how request_info can be correct, but the response contains an altered session_id. I even dumbed it down to one single request and it still changes the session_id?

    async def fetch_one(session: aiohttp.ClientSession, favorite_slug: str):

    await asyncio.sleep(random.uniform(DELAY_MIN, DELAY_MAX))

    payload = {
    "query": QUERY,
    "variables": {
    "favoriteSlug": favorite_slug,
    "skip" : 0,
    "limit" : 100,
    "filtersV2" : {"filterCombineType": "ALL"},
    },
    }



    async with session.post(GRAPHQL, json=payload, headers=HEADERS, cookies=COOKIE) as response:
    body = await response.json()

    if "errors" in body:
    raise RuntimeError(body["errors"])

    return favorite_slug, body["data"]["favoriteQuestionList"]


    async def worker(sem: asyncio.Semaphore,
    session: aiohttp.ClientSession,
    favorite_slug: str):
    """basically controls number of concurrent requests"""
    async with sem:
    slug, data = await fetch_one(session, favorite_slug)

    out_file = OUTDIR / f"{slug}.json"
    out_file.write_text(json.dumps(data, indent=2))
    print(f"✓ {slug:<35} {data['totalLength']:>3} questions")


    async def main(companies: List[str]):
    sem = asyncio.Semaphore(CONCURRENCY)

    async with aiohttp.ClientSession(cookies=COOKIE, headers=HEADERS) as sess:
    tasks = [
    asyncio.create_task(worker(sem, sess, c + suf))
    for c in companies
    for suf in WINDOW_SUFFIXES.values()
    ]
    await asyncio.gather(*tasks)


    Here is the cookie and header packet if you were wondering:

    COOKIE = {"csrftoken": csrftoken, "LEETCODE_SESSION": session_id}

    HEADERS = {"x-csrftoken": csrftoken,
    "Referer": "https://leetcode.com",
    "Content-Type": "application/json"}


    I wish I could post the exact outputs but that would be pretty stupid of me but happy to follow up if anyone has additional information they need.

    Continue reading...

Compartilhe esta Página