Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable cookie storage in AsyncSession #477

Open
omar6995 opened this issue Jan 15, 2025 · 1 comment
Open

Disable cookie storage in AsyncSession #477

omar6995 opened this issue Jan 15, 2025 · 1 comment
Labels
question Ask for help or clarification

Comments

@omar6995
Copy link

Hello everyone,

I'm looking for a way to not store cookies in an AsyncSession.

I have the following code , but i noticed that cookies are stored between requests. When the site i'm trying to scrape detect i'm a bot it stores a cookie that is used again by all the requests of the session. I'd like to disable cookie storage.

Is there an easy way to do it ?

Thanks in advance

async with AsyncSession(
            verify=False,
            impersonate='chrome',
            headers={'Connection': 'close'},
        ) as session:
            async def make_single_request(request_id: str, request: Dict[str, Any]) -> Tuple[str, Optional[Response]]:
                method = request.get('method', 'GET')
                url = request['url']
                
                for attempt in range(self.max_retries):
                    try:
                        kwargs = {
                            'params': request.get('params'),
                            'data': request.get('data'),
                            'headers': request.get('headers'),
                            'proxy': self.proxy_url,
                            'referer': request.get('referer')
                        }
                        
                        if method == 'GET':
                            response = await session.get(url, **kwargs)
                        else:
                            response = await session.post(url, **kwargs)
                        
                        if response.status_code in self.retry_status_codes:
                            raise Exception(f"Received status code {response.status_code}")
                            
                        # Handle response encoding
                        #self._handle_response_encoding(response)
                        
                        return request_id, response
@omar6995 omar6995 added the question Ask for help or clarification label Jan 15, 2025
@vgavro
Copy link

vgavro commented Jan 16, 2025

from http.cookiejar import Cookie, CookieJar

class _DummyCookieJar(CookieJar):
    def set_cookie(self, _cookie: Cookie) -> None:
        return

async with AsyncSession(cookies=_DummyCookieJar()):
   ...

https://github.com/vgavro/httpx-curl_cffi/blob/3d74a7a1324dfcdee3f332caf623003c0829c177/httpx_curl_cffi/transport.py#L206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask for help or clarification
Projects
None yet
Development

No branches or pull requests

2 participants