本文整理匯總了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
示例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
示例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'
)
示例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))
示例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
示例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
示例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
示例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
示例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
)
示例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)
示例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
示例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()