aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
14.8k stars 1.97k forks source link

BUG ? Aiohttp won't keep the same session between requests #3643

Closed SneakerSociety closed 4 years ago

SneakerSociety commented 5 years ago

Long story short

I've been trying to write a code that has two requests. First is a post and second is a get request on the redirect from the post request. I'm getting a 404 error on the get request. It seems like cookies aren't saving between the two requests or two separate sessions are created.

Expected behaviour

I expected two requests to be in the same session.

Actual behaviour

I was trying to make a script that would sign up for an account on cncpts.com. When you try and sign up more than once on an IP it redirects you to a link cncpts.com/challenge which is where you solve a captcha. Anyways, if you try to go to /challenge without making an account first you get a 404 error. So I have two requests, first a post request to fill out the sign up information and then a get request on /challenge so I can parse it for a token I need. I was assuming that with aiohttp sessions are saved like requests but whenever I run the script I get a 404 error as if the requests are being sent with different cookies/sessions.

I've been asking around and no one seems to know. You guys are my last hope. I was told it might be a bug or something, not 100% sure.

Your environment

Current aiohttp version, Windows Client

Steps to reproduce


import random
import names
import discord
import asyncio
from bs4 import BeautifulSoup

async def generator():
    session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False))
    first = names.get_first_name(gender= 'male')
    last = names.get_last_name()
    catchall = '@gmail.com'
    random_number = random.randint(1,10000)
    global email
    email = last+f'{random_number}'+catchall
    password = 'Solefood9473'

    headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Referer': 'https://cncpts.com/account/login',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
    }

    info = {
    'form_type': 'create_customer',
    'utf8': '✓',
    'customer[first_name]': first,
    'customer[last_name]': last,
    'customer[email]': email,
    'customer[password]': password
    }

    async with session.post('https://cncpts.com/account', data=info, headers=headers) as resp:
        if str(resp.url) == 'https://cncpts.com/':
            print('Successfully signed up!')
            print(f'{email}:{password}')

        else:
            print('Captcha needed, submitting now...')
            async with session.get('https://cncpts.com/challenge') as resp:
                url = await resp.text()
                print(url)

                soup = BeautifulSoup(url, 'html.parser')
                auth_val = soup.findAll("input", {"name": "authenticity_token"})
                auth_final = auth_val[0]["value"]
                print(auth_final)

        await session.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(generator())
ghost commented 5 years ago

Have similar errors when I work with some sites.

odysseusmax commented 5 years ago

I too experience this issue. In requests made with same session, cookies are lost, and the requests are treated as separate new requests. Hope will be fixed soon

blueyed commented 5 years ago

This needs more details, e.g. cookies being lost. It might be due to cookies not getting parsed/recognized in the first place (see https://github.com/aio-libs/aiohttp/issues/2502#issuecomment-488864745).

asvetlov commented 4 years ago

Close, people don't provide required info