How to use the aiohttp.TCPConnector function in aiohttp

To help you get started, we’ve selected a few aiohttp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Python3WebSpider / ProxyPool / proxypool / processors / tester.py View on Github external
async def test(self, proxy: Proxy):
        """
        test single proxy
        :param proxy: Proxy object
        :return:
        """
        async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
            try:
                logger.debug(f'testing {proxy.string()}')
                # if TEST_ANONYMOUS is True, make sure that
                # the proxy has the effect of hiding the real IP
                if TEST_ANONYMOUS:
                    url = 'https://httpbin.org/ip'
                    async with session.get(url, timeout=TEST_TIMEOUT) as response:
                        resp_json = await response.json()
                        origin_ip = resp_json['origin']
                    async with session.get(url, proxy=f'http://{proxy.string()}', timeout=TEST_TIMEOUT) as response:
                        resp_json = await response.json()
                        anonymous_ip = resp_json['origin']
                    assert origin_ip != anonymous_ip
                    assert proxy.host == anonymous_ip
                async with session.get(TEST_URL, proxy=f'http://{proxy.string()}', timeout=TEST_TIMEOUT,
                                       allow_redirects=False) as response:
github foglamp / FogLAMP / tests / unit / python / foglamp / services / core / api / test_common_ping.py View on Github external
app = web.Application(loop=loop, middlewares=[middleware.auth_middleware])
                    # fill route table
                    routes.setup(app)

                    server = await aiohttp_server(app, ssl=ssl_ctx)
                    await server.start_server(loop=loop)

                    with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                        client = await aiohttp_client(server)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                    with pytest.raises(aiohttp.ClientConnectorSSLError) as error_exec:
                        # self signed certificate,
                        # and we are not using SSL context here for client as verifier
                        connector = aiohttp.TCPConnector(verify_ssl=True, loop=loop)
                        client = await aiohttp_client(server, connector=connector)
                        await client.get('/foglamp/ping')
                    assert "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed" in str(error_exec)

                    connector = aiohttp.TCPConnector(verify_ssl=False, loop=loop)
                    client = await aiohttp_client(server, connector=connector)
                    resp = await client.get('/foglamp/ping')
                    s = resp.request_info.url.human_repr()
                    assert "https" == s[:5]
                    assert 200 == resp.status
                    content = await resp.text()
                    content_dict = json.loads(content)
                    assert 0 <= content_dict["uptime"]
                    assert 2 == content_dict["dataRead"]
                    assert 100 == content_dict["dataSent"]
                    assert 1 == content_dict["dataPurged"]
github apple-han / flask-reptiles / base / proxypool / tester.py View on Github external
async def test_single_proxy(self, proxy):
        """
        测试单个代理
        :param proxy:
        :return:
        """
        conn = aiohttp.TCPConnector(verify_ssl=False)
        async with aiohttp.ClientSession(connector=conn) as session:
            try:
                if isinstance(proxy, bytes):
                    proxy = proxy.decode('utf-8')
                real_proxy = 'http://' + proxy
                print('正在测试', proxy)
                async with session.get(TEST_URL, proxy=real_proxy, timeout=15, allow_redirects=False) as response:
                    if response.status in VALID_STATUS_CODES:
                        self.redis.max(proxy)
                        print('代理可用', proxy)
                    else:
                        self.redis.decrease(proxy)
                        print('请求响应码不合法 ', response.status, 'IP', proxy)
            except (ClientError, aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, AttributeError):
                self.redis.decrease(proxy)
                print('代理请求失败', proxy)
github closeio / socketshark / socketshark / utils.py View on Github external
async def http_post(shark, url, data):
    opts = shark.config['HTTP']
    if opts.get('ssl_cafile'):
        ssl_context = ssl.create_default_context(cafile=opts['ssl_cafile'])
    else:
        ssl_context = None
    conn = aiohttp.TCPConnector(ssl_context=ssl_context)
    async with aiohttp.ClientSession(connector=conn) as session:
        for n in range(opts['tries']):
            if n > 0:
                await asyncio.sleep(opts['wait'])
            try:
                shark.log.debug('http request', url=url, data=data)
                async with session.post(url, json=data,
                                        timeout=opts['timeout']) as resp:
                    resp.raise_for_status()
                    data = await resp.json()
                    shark.log.debug('http response', data=data)
                    return data
            except aiohttp.ClientError:
                shark.log.exception('unhandled exception in http_post')
            except asyncio.TimeoutError:
                shark.log.exception('timeout in http_post')
github fluentpython / concurrency2017 / lab-flags / flags_await.py View on Github external
async def download_many(loop, cc_list):
    tcpconnector = aiohttp.TCPConnector(family=socket.AF_INET)
    async with aiohttp.ClientSession(connector=tcpconnector) as client:
    # async with aiohttp.ClientSession(loop=loop) as client:  # <8>
        to_do = [download_one(client, cc) for cc in sorted(cc_list)]  # <9>
        res = await asyncio.gather(*to_do)
    return len(res)  # <10>
github pulp / pulp / plugin / pulpcore / plugin / download / factory.py View on Github external
)
        else:
            if self._remote.ssl_client_key.name and self._remote.ssl_client_certificate.name:
                sslcontext = ssl.create_default_context()
                sslcontext.load_cert_chain(
                    self._remote.ssl_client_key.name,
                    self._remote.ssl_client_certificate.name
                )

        if sslcontext:
            tcp_conn_opts['ssl_context'] = sslcontext

        if self._remote.ssl_validation:
            tcp_conn_opts['verify_ssl'] = self._remote.ssl_validation

        conn = aiohttp.TCPConnector(**tcp_conn_opts)

        auth_options = {}
        if self._remote.username and self._remote.password:
            auth_options['auth'] = aiohttp.BasicAuth(
                login=self._remote.username,
                password=self._remote.password
            )

        timeout = aiohttp.ClientTimeout(total=None, sock_connect=600, sock_read=600)
        return aiohttp.ClientSession(connector=conn, timeout=timeout, **auth_options)
github appditto / pippin_nano_wallet / pippin / network / work_client.py View on Github external
def instance(cls) -> 'WorkClient':
        if cls._instance is None:
            cls._instance = cls.__new__(cls)
            cls.work_urls = config.Config.instance().work_peers
            if config.Config.instance().node_work_generate:
                cls.work_urls.append(config.Config.instance().node_url)
            cls.connector = aiohttp.TCPConnector(family=0 ,resolver=aiohttp.AsyncResolver())
            cls.session = aiohttp.ClientSession(connector=cls.connector, json_serialize=json.dumps)
            cls.active_difficulty = nanopy.work_difficulty
            cls.dpow_client = None
            cls.dpow_futures = {}
            cls.dpow_id = 1
            # Construct DPoW Client
            cls.dpow_user = os.getenv('DPOW_USER', None)
            cls.dpow_key = os.getenv('DPOW_KEY', None)
            if cls.dpow_user is not None and cls.dpow_key is not None:
                cls.dpow_client = DpowClient(
                    cls.dpow_user,
                    cls.dpow_key,
                    work_futures=cls.dpow_futures,
                    bpow=False
                )
                cls.dpow_fallback_url = 'https://dpow.nanocenter.org/service/'
github technologiescollege / Blockly-at-rduino / supervision / s2aio / Lib / site-packages / aiohttp / client.py View on Github external
does not have Content-Length header.
    :param request_class: (optional) Custom Request class implementation.
    :param response_class: (optional) Custom Response class implementation.
    :param loop: Optional event loop.

    Usage::

      >>> import aiohttp
      >>> resp = yield from aiohttp.request('GET', 'http://python.org/')
      >>> resp
      
      >>> data = yield from resp.read()

    """
    if connector is None:
        connector = aiohttp.TCPConnector(loop=loop, force_close=True)

    kwargs = {}

    if request_class is not None:
        kwargs['request_class'] = request_class

    if response_class is not None:
        kwargs['response_class'] = response_class

    session = ClientSession(loop=loop,
                            cookies=cookies,
                            connector=connector,
                            **kwargs)
    return _DetachedRequestContextManager(
        session._request(method, url,
                         params=params,
github hangoutsbot / hangoutsbot / hangupsbot / plugins / _chatbridge / chatbridge_telegram.py View on Github external
def telegram_api_request(self, configuration, method, data):
        connector = aiohttp.TCPConnector(verify_ssl=True)
        headers = {'content-type': 'application/x-www-form-urlencoded'}

        BOT_API_KEY = configuration["bot_api_key"]

        url = "https://api.telegram.org/bot{}/{}".format(BOT_API_KEY, method)

        response = yield from aiohttp.request('post', url, data=data, headers=headers, connector=connector)
        results = yield from response.text()

        return results
github VirusTotal / vt-py / vt / client.py View on Github external
def _get_session(self):
    if not self._session:
      self._session = aiohttp.ClientSession(
        connector=aiohttp.TCPConnector(ssl=False),
        headers={
            'X-Apikey': self._apikey,
            'Accept-Encoding': 'gzip',
            'User-Agent': _USER_AGENT_FMT.format_map({
                'agent': self._agent, 'version': __version__})})
    return self._session