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


Python client.ClientSession方法代码示例

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


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

示例1: __post

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def __post(self, payload):
        header = {'Content-type': 'application/json'}
        try:
            if self._session is None:
                async with ClientSession() as session:
                    async with session.post(self._url, json=payload, headers=header, timeout=10) as response:
                        res = json.loads(await response.content.read(-1))
            else:
                async with self._session.post(self._url, json=payload, headers=header, timeout=10) as response:
                    res = json.loads(await response.content.read(-1))
            if res['error'] != 0:
                if res['result'] != '':
                    raise SDKException(ErrorCode.other_error(res['result']))
                else:
                    raise SDKException(ErrorCode.other_error(res['desc']))
        except (asyncio.TimeoutError, client_exceptions.ClientConnectorError):
            raise SDKException(ErrorCode.connect_timeout(self._url)) from None
        return res 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:20,代码来源:aiorpc.py

示例2: __get

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def __get(self, url):
        try:
            if self.__session is None:
                async with ClientSession() as session:

                    async with session.get(url, timeout=10) as response:
                        res = json.loads(await response.content.read(-1))
            else:
                async with self.__session.get(url, timeout=10) as response:
                    res = json.loads(await response.content.read(-1))
            if res['Error'] != 0:
                if res['Result'] != '':
                    raise SDKException(ErrorCode.other_error(res['Result']))
                else:
                    raise SDKException(ErrorCode.other_error(res['Desc']))
            return res
        except (asyncio.TimeoutError, client_exceptions.ClientConnectorError):
            raise SDKException(ErrorCode.connect_timeout(self._url)) from None 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:20,代码来源:aiorestful.py

示例3: _make_request

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def _make_request(self, context:FContext, payload):
		sem = asyncio.Semaphore(200)
		conn = aiohttp.TCPConnector(use_dns_cache=True, loop=self.loop, limit=0)
		async with sem:
			async with ClientSession(connector=conn) as session:
				try:
					if self._timeout > 0:
						with async_timeout.timeout(self._timeout / 1000):
							async with session.post(self._url, 
												data=payload,
												headers=self._headers) \
								as response:
								return response.status, await response.content.read()
					else:
						async with session.post(self._url,data=payload,headers=self._headers) as response:
							return response.status, await response.content.read()
				except asyncio.TimeoutError:
					raise TTransportException(
						type=TTransportExceptionType.TIMED_OUT,
						message='request timed out'
						) 
开发者ID:dyseo,项目名称:AsyncLine,代码行数:23,代码来源:http_client.py

示例4: __init__

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def __init__(
        self,
        route_info: Dict,
        url: str,
        username: str = None,
        password: str = None,
        bucket_class: Type[Bucket] = Bucket,
    ) -> None:
        self.url = url
        self.session = ClientSession(timeout=timeout)
        self.bucket = bucket_class(size=route_info["options"]["bulk_size"])
        self.route_info = route_info
        self._handler = route_info["handler"]
        self.username = username
        self.password = password
        self.routes: List[str] = []
        for route in self.route_info["routes"]:
            self.routes.append(urljoin(self.url, route)) 
开发者ID:b2wdigital,项目名称:async-worker,代码行数:20,代码来源:consumer.py

示例5: __init__

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def __init__(self, url: str = '', qid: int = 0, session: ClientSession = None):
        super().__init__(url, qid)
        self._session = session 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:5,代码来源:aiorpc.py

示例6: session

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def session(self, session: ClientSession):
        if not isinstance(session, ClientSession):
            raise SDKException(ErrorCode.param_error)
        self._session = session 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:6,代码来源:aiorpc.py

示例7: __init__

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def __init__(self, url: str = '', session: ClientSession = None):
        super().__init__(url)
        self.__session = session 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:5,代码来源:aiorestful.py

示例8: session

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def session(self, session: ClientSession):
        if not isinstance(session, ClientSession):
            raise SDKException(ErrorCode.param_error)
        self.__session = session 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:6,代码来源:aiorestful.py

示例9: from_url

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def from_url(cls, url, *, user, connection=None):
        async with client.ClientSession() as session:
            async with session.get(url) as response:
                if response.status != 200:
                    return
                l = response.content_length
                if not l or l > 2 ** 23:
                    return
                content_type = response.content_type
                if not content_type.startswith('image'):
                    return
                content = BytesIO(await response.read())
                filename = response.url.name
        exts = mimetypes.guess_all_extensions(content_type)
        for ext in exts:
            if filename.endswith(ext):
                break
        else:
            if exts:
                filename += exts[-1]
        name = await cls.app.loop.run_in_executor(
            None, image_storage.save, filename, content)
        image_uuid = image_storage.uuid(name)
        return await cls.create(
            uuid=image_uuid,
            image=name,
            mime_type=content_type,
            created_at=utils.now(),
            author_id=user.pk,
            connection=connection
        ) 
开发者ID:dvhb,项目名称:dvhb-hybrid,代码行数:33,代码来源:amodels.py

示例10: test_add_handler_for_metrics_endpoint

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def test_add_handler_for_metrics_endpoint(self, cleanup):
        await self.signal_handler.startup(self.app)

        async with ClientSession() as client:
            async with client.get(
                f"http://{settings.HTTP_HOST}:{settings.HTTP_PORT}{settings.METRICS_ROUTE_PATH}"
            ) as resp:
                self.assertEqual(200, resp.status) 
开发者ID:b2wdigital,项目名称:async-worker,代码行数:10,代码来源:test_http.py

示例11: _connect

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def _connect(self, session: ClientSession) -> ClientResponse:
        response = await session.get(
            self.url, headers={"Accept": "text/event-stream"}
        )
        await self.on_connection()
        return response 
开发者ID:b2wdigital,项目名称:async-worker,代码行数:8,代码来源:consumer.py

示例12: setUp

# 需要导入模块: from aiohttp import client [as 别名]
# 或者: from aiohttp.client import ClientSession [as 别名]
def setUp(self):
        self.url = 'http://example.com/api?foo=bar#fragment'
        self.session = ClientSession()
        super().setUp() 
开发者ID:pnuckowski,项目名称:aioresponses,代码行数:6,代码来源:test_aioresponses.py


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