Skip to content

[feature required] does we can add the timeout or proxy parameters directly #711

Closed

Description

@a358003542

Like this, we will directly have the proxy and timeout parameters for the ClientSession, this will be very convenient in some situation.

class Client(aiohttp.ClientSession):
    '''
based on aiohttp's ClientSession,and add:
- proxy proxy_auth (user,pass)
- timeout

    '''
    def __init__(self,timeout=None,proxy=None,proxy_auth=None,*args,**kwargs):
        self.timeout = timeout

        conn = None
        if proxy:### handle proxy
            if proxy_auth:
                conn = aiohttp.ProxyConnector(proxy=proxy,proxy_auth=aiohttp.BasicAuth(proxy_auth[0], proxy_auth[1]))
            else:
                conn = aiohttp.ProxyConnector(proxy=proxy)

        super().__init__(connector=conn,*args,**kwargs)


    def _request(self,*args,**kwargs):
        return asyncio.wait_for(super()._request(*args,**kwargs), self.timeout)

    def _ws_connect(self,*args,**kwargs):
        return asyncio.wait_for(super()._ws_connect(*args,**kwargs), self.timeout)

Activity

  1. asvetlov commented on Dec 28, 2015

    @asvetlov
    Member

    Sorry, I've declined your feature request.

    1. Connector has much more options than just proxy_auth. Moreover, params for TCPConnector and UnixConnector are ambiguous. Putting all together makes a mess. Recommended way to create session with proxy is ClientSession(connector=ProxyConnector(proxy, proxy_auth=BasicAuth(login, password)).
      You still may use your own Client class provided above in your code.
    2. Proposed timeout parameter works only for connection establishment and sending request headers.
      It doesn't include time for sending request body and retrieving response back.
      Most likely users need both.
      The recommended way is using Timeout class:
        with aiohttp.Timeout(0.001):
            async with aiohttp.get('https://github.com') as r:
                await r.text()
    
  2. a358003542 commented on Dec 28, 2015

    @a358003542
    Author

    OK, listening and learned.

  3. lock commented on Oct 29, 2019

    @lock

    This thread has been automatically locked since there has not been
    any recent activity after it was closed. Please open a new issue for
    related bugs.

    If you feel like there's important points made in this discussion,
    please include those exceprts into that new issue.

  4. locked as resolved and limited conversation to collaborators on Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions