当前位置: 首页>>代码示例>>Python>>正文


Python aiohttp.ClientSession类代码示例

本文整理汇总了Python中aiohttp.ClientSession的典型用法代码示例。如果您正苦于以下问题:Python ClientSession类的具体用法?Python ClientSession怎么用?Python ClientSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ClientSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: go

        def go(dirname, filename):
            ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            ssl_ctx.load_cert_chain(
                os.path.join(dirname, 'sample.crt'),
                os.path.join(dirname, 'sample.key')
            )
            app, _, url = yield from self.create_server(
                'GET', '/static/' + filename, ssl_ctx=ssl_ctx
            )
            app.router.add_static('/static', dirname)

            conn = TCPConnector(verify_ssl=False, loop=self.loop)
            session = ClientSession(connector=conn)

            resp = yield from session.request('GET', url)
            self.assertEqual(200, resp.status)
            txt = yield from resp.text()
            self.assertEqual('file content', txt.rstrip())
            ct = resp.headers['CONTENT-TYPE']
            self.assertEqual('application/octet-stream', ct)
            self.assertEqual(resp.headers.get('CONTENT-ENCODING'), None)
            resp.close()

            resp = yield from session.request('GET', url + 'fake')
            self.assertEqual(404, resp.status)
            resp.close()

            resp = yield from session.request('GET', url + '/../../')
            self.assertEqual(404, resp.status)
            resp.close()
开发者ID:danielnelson,项目名称:aiohttp,代码行数:30,代码来源:test_web_functional.py

示例2: github_action

def github_action(action, plugin, config, sort_by='updated'):
    url = API_URL.format(
        api=API_URL,
        user=config['user'],
        repo=config['repository'],
        action=action,
    )
    query = {
        'sort': sort_by,
        'direction': 'desc',
        'sha': config.get('branch', 'master')
    }
    headers = {'Accept': 'application/vnd.github.v3+json'}
    etag = plugin.temp.get(action)
    if etag is not None:
        headers['If-None-Match'] = etag

    session = ClientSession()
    try:
        resp = yield from asyncio.wait_for(
            session.get(url, params=query, headers=headers),
            timeout=5
        )
        try:
            plugin.temp[action] = resp.headers.get('etag')
            if resp.status != 200 or etag is None:  # etag must be cached first
                raise NothingChangeException(etag)
            data = yield from resp.json()
        finally:
            resp.close()
    finally:
        session.close()
    return data[0]
开发者ID:andrzej3393,项目名称:grazyna,代码行数:33,代码来源:github.py

示例3: go

 def go():
     _, srv, url = yield from self.create_server(None, '/', None)
     client = ClientSession(loop=self.loop)
     resp = yield from client.get(url)
     self.assertEqual(404, resp.status)
     yield from resp.release()
     client.close()
开发者ID:adamchainz,项目名称:aiohttp,代码行数:7,代码来源:test_web_functional.py

示例4: go

 def go():
     _, srv, url = yield from self.create_server('GET', '/', handler)
     client = ClientSession(loop=self.loop)
     resp = yield from client.get(url)
     self.assertEqual(200, resp.status)
     data = yield from resp.read()
     self.assertEqual(b'xyz', data)
     yield from resp.release()
开发者ID:vshulyak,项目名称:aiohttp,代码行数:8,代码来源:test_web_functional.py

示例5: go

 def go():
     _, srv, url = yield from self.create_server('GET', '/', handler)
     client = ClientSession(loop=self.loop)
     resp = yield from client.get(url)
     self.assertEqual(200, resp.status)
     data = yield from resp.read()
     self.assertEqual(b'mydata', data)
     self.assertEqual(resp.headers.get('CONTENT-ENCODING'), 'deflate')
     yield from resp.release()
     client.close()
开发者ID:mind1master,项目名称:aiohttp,代码行数:10,代码来源:test_web_functional.py

示例6: get_html

def get_html(url):
    session = ClientSession()
    try:
        resp = yield from session.get(url)
        try:
            raw_data = yield from resp.read()
            data = raw_data.decode('utf-8', errors='xmlcharrefreplace')
            return fromstring_to_html(data)
        finally:
            resp.close()
    finally:
        session.close()
开发者ID:andrzej3393,项目名称:grazyna,代码行数:12,代码来源:hs_wiki.py

示例7: check

    async def check(self, aio_client: aiohttp.ClientSession,
                    usernames: AbstractSet[str]) -> ni_abc.Status:
        base_url = "https://bugs.python.org/[email protected]=clacheck&github_names="
        url = base_url + ','.join(usernames)
        self.server.log("Checking CLA status: " + url)
        async with aio_client.get(url) as response:
            if response.status >= 300:
                msg = f'unexpected response for {response.url!r}: {response.status}'
                raise client.HTTPException(msg)
            # Explicitly decode JSON as b.p.o doesn't set the content-type as
            # `application/json`.
            results = json.loads(await response.text())
        self.server.log("Raw CLA status: " + str(results))
        status_results = [results[k] for k in results.keys() if k in usernames]
        self.server.log("Filtered CLA status: " + str(status_results))
        if len(status_results) != len(usernames):
            raise ValueError("# of usernames don't match # of results "
                             "({} != {})".format(len(usernames), len(status_results)))
        elif any(x not in (True, False, None) for x in status_results):
            raise TypeError("unexpected value in " + str(status_results))

        if all(status_results):
            return ni_abc.Status.signed
        elif any(value is None for value in status_results):
            return ni_abc.Status.username_not_found
        else:
            return ni_abc.Status.not_signed
开发者ID:python,项目名称:the-knights-who-say-ni,代码行数:27,代码来源:bpo.py

示例8: __init__

 def __init__(self, configuration):
     self._limit_sleep_time_coefficient = configuration \
         .instagram_limit_sleep_time_coefficient
     self._limit_sleep_time_min = configuration \
         .instagram_limit_sleep_time_min
     self._success_sleep_time_coefficient = configuration \
         .instagram_success_sleep_time_coefficient
     self._success_sleep_time_max = configuration \
         .instagram_success_sleep_time_max
     self._success_sleep_time_min = configuration \
         .instagram_success_sleep_time_min
     self._limit_sleep_time = self._limit_sleep_time_min
     self._success_sleep_time = self._success_sleep_time_max
     self._username = configuration.instagram_username
     self._password = configuration.instagram_password
     self._referer = BASE_URL
     self._session = ClientSession(
         cookies={
             'ig_pr': '1',
             'ig_vw': '1920',
             },
         headers={
             'User-Agent': USER_AGENT,
             'X-Instagram-AJAX': '1',
             'X-Requested-With': 'XMLHttpRequest',
             },
         )
     loop = asyncio.get_event_loop()
     loop.run_until_complete(self._do_login())
开发者ID:quasiyoke,项目名称:InstaBot,代码行数:29,代码来源:instagram.py

示例9: query_imdb

async def query_imdb(*, imdb_id: Optional[int],
                     year: int,
                     session: ClientSession) -> Dict[str, Any]:
    params = dict(i=f'tt{imdb_id:0>{IMDB_ID_LENGTH}}',
                  y=year,
                  plot='full',
                  tomatoes='true',
                  r='json')
    while True:
        attempt_num = 0
        async with session.get(IMDB_API_URL, params=params) as response:
            attempt_num += 1
            if response.status == A_TIMEOUT_OCCURRED:
                logger.debug(f'Attempt #{attempt_num} failed: '
                             f'server "{IMDB_API_URL}" answered with '
                             f'status code {A_TIMEOUT_OCCURRED}. '
                             f'Waiting {RETRY_INTERVAL_IN_SECONDS} second(s) '
                             'before next attempt.')
                await sleep(RETRY_INTERVAL_IN_SECONDS)
                continue
            try:
                response_json = await response.json()
            except JSONDecodeError:
                logger.exception('')
                return dict()
            return response_json
开发者ID:lycantropos,项目名称:RecommendSystem,代码行数:26,代码来源:service.py

示例10: _fetch_url

async def _fetch_url(
        url: str,
        session: aiohttp.ClientSession,
        timeout: float = 10.0) -> str:
    with async_timeout.timeout(timeout):
        async with session.get(url) as response:
            response.raise_for_status()
            return await response.text()
开发者ID:butla,项目名称:experiments,代码行数:8,代码来源:html_extractor.py

示例11: fetch_with_sleep

async def fetch_with_sleep(session: aiohttp.ClientSession,
                           url: str,
                           sleep_time: int) -> Response:
    async with session.get(url) as response:
        print(f"request {url} with {sleep_time}s")
        await sleep(sleep_time)
        print(f"request {url} sleep done")
        return response
开发者ID:JungWinter,项目名称:Code_Study,代码行数:8,代码来源:async_await.py

示例12: __init__

class Bot:
    '''Base class of your bot.

    You should implement the ``on_message`` function.
    
    Text may be parsed using ``parse``
    '''
    def __init__(self):
        self.conversations = {}
        self.history = []
        self._client = ClientSession()

    def __enter__(self):
        return self

    def __exit__(self, ex_type, ex_value, ex_tb):
        self._client.close()

    def close(self):
        self._client.close()

    async def parse(self, text):
        async with self._client.post(
            LUIS_ENDPOINT,
            text.encode('utf-8'),
        ) as resp:
            data = await resp.json()
        
        # TODO: Extract relevant information from data
        return data

    @abstractmethod
    async def on_message(self, conversation, text):
        pass

    async def _handler(self, request):
        return web.Response(
            b"Not implemented",
        )

    async def _history(self, request):
        return web.Response(
            '\n'.join('<p>{!r}</p>'.format(h) for h in self.history).encode('utf-8')
        )
开发者ID:hradoi,项目名称:botframework-python,代码行数:44,代码来源:bot.py

示例13: Downloader

class Downloader(Actor):
    async def startup(self):
        self.session = ClientSession(loop=self.loop)

    @concurrent
    async def download_content(self, url):
        async with self.session.get(url) as response:
            content = await response.read()
            print('{}: {:.80}...'.format(url, content.decode()))
        return len(content)

    async def shutdown(self):
        self.session.close()

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc, tb):
        await self.close()
开发者ID:podhmo,项目名称:individual-sandbox,代码行数:19,代码来源:00downloader.py

示例14: afetch

async def afetch(session: ClientSession, url: str):
    """
    Asynchronous fetch.  Do a GET request,  return text,  properly  shutdown

    :param session: ClientSession object for aiohttp connection
    :param url: The URL we want
    :return:
    """
    async with session.get(url) as response:
        return await response.text()
开发者ID:paulhoule,项目名称:tentacruel,代码行数:10,代码来源:__init__.py

示例15: process_partition

async def process_partition(
    loop: asyncio.BaseEventLoop,
    results_queue: asyncio.Queue,
    server_address: URL,
    http: aiohttp.ClientSession,
    partition: PointsPartition,
    mission_template: Template,
    mission_loader: str,
    mission_name: str,
    width: int,
    scale: int,
) -> Awaitable[None]:
    LOG.debug(
        f"query range [{partition.start}:{partition.end}] on server "
        f"{server_address}"
    )

    file_name = f"{mission_name}_{partition.start}_{partition.end}.mis"
    missions_url = server_address / "missions"
    mission_dir_url = missions_url / "heightmap"
    mission_url = mission_dir_url / file_name

    points = (
        index_to_point(i, width, scale)
        for i in range(partition.start, partition.end + 1)
    )
    mission = mission_template.render(
        loader=mission_loader,
        points=points,
    )

    data = FormData()
    data.add_field(
        'mission',
        mission.encode(),
        filename=file_name,
        content_type='plain/text',
    )

    await http.post(mission_dir_url, data=data)
    await http.post(mission_url / "load")
    await http.post(missions_url / "current" / "begin")

    async with http.get(server_address / "radar" / "stationary-objects") as response:
        data = await response.json()
        data = [
            pack(HEIGHT_PACK_FORMAT, int(point['pos']['z']))
            for point in data
        ]
        data = b''.join(data)

    await http.post(missions_url / "current" / "unload")
    await http.delete(mission_url)

    await results_queue.put((partition, data))
开发者ID:IL2HorusTeam,项目名称:il2-heightmap-creator,代码行数:55,代码来源:creation.py


注:本文中的aiohttp.ClientSession类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。