python类TCPConnector()的实例源码

conftest.py 文件源码 项目:peony-twitter 作者: odrling 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def fixture_medias(event_loop):
    if os.environ.get('FORCE_IPV4', False):
        connector = aiohttp.TCPConnector(family=socket.AF_INET)
    else:
        connector = aiohttp.TCPConnector()
    with aiohttp.ClientSession(loop=event_loop,
                               connector=connector) as session:
        task = asyncio.gather(*[media.download(session=session)
                                for media in medias.values()])
        event_loop.run_until_complete(task)

    return medias
bridge.py 文件源码 项目:xmppwb 作者: saqura 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _parse_outgoing_webhooks(self, bridge_cfg):
        """Parses the `outgoing webhooks` from this bridge's config file
        section.

        This also sets up the HTTP client session for each webhook."""
        if 'outgoing_webhooks' not in bridge_cfg:
            # No outgoing webhooks in this bridge.
            return

        outgoing_webhooks = bridge_cfg['outgoing_webhooks']

        for outgoing_webhook in outgoing_webhooks:
            if 'url' not in outgoing_webhook:
                raise InvalidConfigError("Error in config file: "
                                         "'url' is missing from an "
                                         "outgoing webhook definition.")

            # Set up SSL context for certificate pinning.
            if 'cafile' in outgoing_webhook:
                cafile = os.path.abspath(outgoing_webhook['cafile'])
                sslcontext = ssl.create_default_context(cafile=cafile)
                conn = aiohttp.TCPConnector(ssl_context=sslcontext)
                session = aiohttp.ClientSession(loop=self.main_bridge.loop,
                                                connector=conn)
            else:
                session = aiohttp.ClientSession(loop=self.main_bridge.loop)
            # TODO: Handle ConnectionRefusedError.
            outgoing_webhook['session'] = session

            self.outgoing_webhooks.append(outgoing_webhook)
rest.py 文件源码 项目:py-restfmclient 作者: pcdummy 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def session(self):
        if self._session is None:
            conn = aiohttp.TCPConnector(
                loop=self._loop, verify_ssl=self.verify_ssl
            )
            self._session = aiohttp.ClientSession(
                loop=self._loop, connector=conn
            )

        return self._session
asyncio.py 文件源码 项目:apm-agent-python 作者: elastic 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(self, parsed_url, **kwargs):
        super(AsyncioHTTPTransport, self).__init__(parsed_url, **kwargs)
        loop = asyncio.get_event_loop()
        session_kwargs = {'loop': loop}
        if not self._verify_server_cert:
            session_kwargs['connector'] = aiohttp.TCPConnector(verify_ssl=False)
        self.client = aiohttp.ClientSession(**session_kwargs)
chatbridge_telegram.py 文件源码 项目:hangoutsbot 作者: das7pad 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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)

        async with aiohttp.ClientSession() as session:
            async with session.post(url, data=data, headers=headers, connector=connector) as response:
                results = await response.text()

        return results
testing.py 文件源码 项目:sanic 作者: channelcat 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _local_request(self, method, uri, cookies=None, *args, **kwargs):
        import aiohttp
        if uri.startswith(('http:', 'https:', 'ftp:', 'ftps://' '//')):
            url = uri
        else:
            url = 'http://{host}:{port}{uri}'.format(
                host=HOST, port=self.port, uri=uri)

        logger.info(url)
        conn = aiohttp.TCPConnector(verify_ssl=False)
        async with aiohttp.ClientSession(
                cookies=cookies, connector=conn) as session:
            async with getattr(
                    session, method.lower())(url, *args, **kwargs) as response:
                try:
                    response.text = await response.text()
                except UnicodeDecodeError as e:
                    response.text = None

                try:
                    response.json = await response.json()
                except (JSONDecodeError,
                        UnicodeDecodeError,
                        aiohttp.ClientResponseError):
                    response.json = None

                response.body = await response.read()
                return response
twhttp.py 文件源码 项目:twtxt 作者: buckket 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_remote_tweets(sources, limit=None, timeout=5.0, cache=None):
    conn = aiohttp.TCPConnector(use_dns_cache=True)
    headers = generate_user_agent()
    with aiohttp.ClientSession(connector=conn, headers=headers, conn_timeout=timeout) as client:
        loop = asyncio.get_event_loop()

        def start_loop(client, sources, limit, cache=None):
            return loop.run_until_complete(process_sources_for_file(client, sources, limit, cache))

        tweets = start_loop(client, sources, limit, cache)

    return tweets
twhttp.py 文件源码 项目:twtxt 作者: buckket 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def get_remote_status(sources, timeout=5.0):
    conn = aiohttp.TCPConnector(use_dns_cache=True)
    headers = generate_user_agent()
    with aiohttp.ClientSession(connector=conn, headers=headers, conn_timeout=timeout) as client:
        loop = asyncio.get_event_loop()
        result = loop.run_until_complete(process_sources_for_status(client, sources))
    return result
updates.py 文件源码 项目:client 作者: syncrypt 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def retrieve_available_version(platform_id):
    sslcontext = ssl.create_default_context(cafile=certifi.where())
    conn = aiohttp.TCPConnector(ssl_context=sslcontext)
    with aiohttp.ClientSession(connector=conn) as c:
        r = yield from c.get(CURRENT_ENDPOINT)
        content = yield from r.json()
        return content[platform_id]
http.py 文件源码 项目:client 作者: syncrypt 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def init_client(self, client, headers={}):
        sslcontext = ssl.create_default_context(cafile=certifi.where())
        conn = aiohttp.TCPConnector(ssl_context=sslcontext)
        if client:
            self.client_owned, self.client = False, client
        else:
            self.client_owned, self.client = True, aiohttp.ClientSession(
                    connector=conn,
                    headers=headers,
                    skip_auto_headers=["Content-Type", "User-Agent"]
                    )
fetch.py 文件源码 项目:async-fetcher 作者: night-crawler 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def __init__(self,
                 task_map: dict, timeout: int = 10, num_retries: int = 0,
                 retry_timeout: float = 1.0,
                 service_name: str = 'api',
                 cafile: str = None,
                 loop: t.Optional[asyncio.AbstractEventLoop] = None,
                 tcp_connector: t.Union[aiohttp.TCPConnector, None] = None,
                 keepalive_timeout: int = 60):
        """
        :param task_map: dict, task bundle mapping like {'task_name': <task_bundle>}
        :param timeout: int, request timeout
        :param num_retries: int, max retry count before exception rising
        :param retry_timeout: float, wait before retry
        :param service_name: str, service name label for verbose logging
        :param keepalive_timeout: int, keepalive timeout for TCPConnector created __internally__
        """
        self.task_map = OrderedDict(task_map.items())
        self.timeout = timeout
        self.num_retries = num_retries
        self.max_retries = num_retries
        self.retry_timeout = retry_timeout
        self.service_name = service_name

        self.cafile = cafile
        self.loop = loop or get_or_create_event_loop()
        self._tcp_connector = tcp_connector
        self._connector_owner = not bool(tcp_connector)

        # keepalive_timeout for __internally__ created connector
        self.keepalive_timeout = keepalive_timeout
session.py 文件源码 项目:build-deploy-stats 作者: luigiberrettini 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __aenter__(self):
        tcp_connector = None if self.verify_ssl_certs else aiohttp.TCPConnector(verify_ssl = False)
        self.session = aiohttp.ClientSession(auth = self.basic_auth_credentials, headers = self.headers, connector = tcp_connector)
        return self
example_test.py 文件源码 项目:overwatch-api 作者: anthok 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def testing(loop):
    # Instantiating the api
    client = AsyncOWAPI()

    data = {}

    # We use our own clientsession to demonstrate that it's possible to pool connections in that way
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
        # We await an api method and get a dict back as a result
        # We pass our session, and we pass the platform we want results for, in this case it's PC and we don't actually need to pass, since it's a default
        print('Testing......[get_profile]')
        data[PC] = await client.get_profile("Danielfrogs#2552", session=session, platform=PC)
        print('Testing......[get_profile]')
        data[XBOX] = await client.get_profile("Danielfrogs#2552", session=session, platform=XBOX)
        print('Testing......[get_profile]')
        data[PLAYSTATION] = await client.get_profile("Danielfrogs#2552", session=session, platform=PLAYSTATION)
        print('Testing......[get_stats]')
        data[PC] = await client.get_stats("Danielfrogs#2552", session=session, platform=PC)
        print('Testing......[get_stats]')
        data[XBOX] = await client.get_stats("Danielfrogs#2552", session=session, platform=XBOX)
        print('Testing......[get_stats]')
        data[PC] = await client.get_stats("Danielfrogs#2552", session=session, platform=PLAYSTATION)
        print('Testing......[get_achievements]')
        data[PC] = await client.get_achievements("Danielfrogs#2552", session=session, platform=PC)
        print('Testing......[get_hero_stats]')
        data[PC] = await client.get_hero_stats("Danielfrogs#2552", session=session, platform=PC)

    print(data)
aiosolr.py 文件源码 项目:aiosolr 作者: TigorC 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, url, decoder=None, timeout=60, results_cls=Results, loop=None):
        if loop is None:
            loop = asyncio.get_event_loop()
        self.loop = loop
        self.decoder = decoder or json.JSONDecoder()
        self.url = url
        self.timeout = timeout
        self.log = self._get_log()
        self.session = aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(use_dns_cache=True, loop=loop),
            loop=loop)
        self.results_cls = results_cls
oweather.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _api_request(self, location):
        payload = {'q': location, 'appid': self.settings['WEATHER_API_KEY']}
        url = 'http://api.openweathermap.org/data/2.5/weather?'
        conn = aiohttp.TCPConnector()
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, params=payload) as r:
            data = await r.json()
        session.close()
        return data
goodreads.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _get_query(self, payload, gateway):
        headers = {'user-agent': 'Red-cog/1.0'}
        conn = aiohttp.TCPConnector(verify_ssl=False)
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(gateway, params=payload, headers=headers) as r:
            data = await r.text()
        session.close()
        return data
lastfm.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _api_request(self, payload):
        url = 'http://ws.audioscrobbler.com/2.0/'
        headers = {'user-agent': 'Red-cog/1.0'}
        conn = aiohttp.TCPConnector()
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, params=payload, headers=headers) as r:
            data = await r.json()
        session.close()
        return data
steam.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _update_apps(self):
        payload = {}
        url = 'http://api.steampowered.com/ISteamApps/GetAppList/v0001/'
        headers = {'user-agent': 'Red-cog/1.0'}
        conn = aiohttp.TCPConnector(verify_ssl=False)
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, params=payload, headers=headers) as r:
            data = await r.json()
        session.close()
        self.games = data['applist']['apps']['app']
        dataIO.save_json('data/steam/games.json', data)
steam.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _app_info(self, gid):
        url = 'http://store.steampowered.com/api/appdetails?'
        payload = {}
        payload['appids'] = gid
        headers = {'user-agent': 'Red-cog/1.0'}
        conn = aiohttp.TCPConnector(verify_ssl=False)
        session = aiohttp.ClientSession(connector=conn)
        async with session.get(url, params=payload, headers=headers) as r:
            data = await r.json()
        session.close()
        if data[str(gid)]['success']:
            data = data[str(gid)]['data']
            info = {}
            info['name'] = data['name']
            info['developers'] = data['developers']
            info['publishers'] = data['publishers']

            if data['is_free']:
                info['price'] = 'Free to Play'
            elif 'price_overview' not in data:
                info['price'] = 'Not available'
            else:
                info['price'] = '{} {}'.format(str(data['price_overview']['final'] / 100), (data['price_overview']['currency']))
                if data['price_overview']['discount_percent'] > 0:
                    info['price'] = '{} {} ({} -{}%)'.format(str(data['price_overview']['final'] / 100), data['price_overview']['currency'], str(data['price_overview']['initial'] / 100), str(data['price_overview']['discount_percent']))
            if data['release_date']['coming_soon']:
                info['release_date'] = 'Coming Soon'
            else:
                info['release_date'] = data['release_date']['date']
            info['genres'] = data['genres']
            info['recommendations'] = ''
            if 'recommendations' in data:
                info['recommendations'] = 'Recommendations: {}\n\n'.format(str(data['recommendations']['total']))
            info['about_the_game'] = re.sub("<.*?>", " ", data['about_the_game'].replace('  ', '').replace('\r', '').replace('<br>', '\n').replace('\t', ''))
            if len(info['about_the_game']) > 500:
                info['about_the_game'] = '{}...'.format(info['about_the_game'][:500-3])
            return info
        return False
wikipedia.py 文件源码 项目:PaddoCogs 作者: PaddoInWonderland 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _wikipedia(self, context, *, query: str):
        """
        Get information from Wikipedia
        """
        try:
            url = 'https://en.wikipedia.org/w/api.php?'
            payload = {}
            payload['action'] = 'query'
            payload['format'] = 'json'
            payload['prop'] = 'extracts'
            payload['titles'] = ''.join(query).replace(' ', '_')
            payload['exsentences'] = '5'
            payload['redirects'] = '1'
            payload['explaintext'] = '1'
            headers = {'user-agent': 'Red-cog/1.0'}
            conn = aiohttp.TCPConnector(verify_ssl=False)
            session = aiohttp.ClientSession(connector=conn)
            async with session.get(url, params=payload, headers=headers) as r:
                result = await r.json()
            session.close()
            if '-1' not in result['query']['pages']:
                for page in result['query']['pages']:
                    title = result['query']['pages'][page]['title']
                    description = result['query']['pages'][page]['extract'].replace('\n', '\n\n')
                em = discord.Embed(title='Wikipedia: {}'.format(title), description=u'\u2063\n{}...\n\u2063'.format(description[:-3]), color=discord.Color.blue(), url='https://en.wikipedia.org/wiki/{}'.format(title.replace(' ', '_')))
                em.set_footer(text='Information provided by Wikimedia', icon_url='https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikimedia-logo.png/600px-Wikimedia-logo.png')
                await self.bot.say(embed=em)
            else:
                message = 'I\'m sorry, I can\'t find {}'.format(''.join(query))
                await self.bot.say('```{}```'.format(message))
        except Exception as e:
            message = 'Something went terribly wrong! [{}]'.format(e)
            await self.bot.say('```{}```'.format(message))


问题


面经


文章

微信
公众号

扫码关注公众号