本文整理汇总了Python中aiohttp.client.ClientSession类的典型用法代码示例。如果您正苦于以下问题:Python ClientSession类的具体用法?Python ClientSession怎么用?Python ClientSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClientSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_close
def test_close(self):
conn = self.make_open_connector()
session = ClientSession(loop=self.loop, connector=conn)
session.close()
self.assertIsNone(session.connector)
self.assertTrue(conn.closed)
示例2: test_init_cookies_with_list_of_tuples
def test_init_cookies_with_list_of_tuples(self):
session = ClientSession(
cookies=[("c1", "cookie1"),
("c2", "cookie2")],
loop=self.loop)
self.assertEqual(set(session.cookies), {'c1', 'c2'})
self.assertEqual(session.cookies['c1'].value, 'cookie1')
self.assertEqual(session.cookies['c2'].value, 'cookie2')
session.close()
示例3: test_detach
def test_detach(self):
session = ClientSession(loop=self.loop)
conn = session.connector
self.assertFalse(conn.closed)
session.detach()
self.assertIsNone(session.connector)
self.assertTrue(session.closed)
self.assertFalse(conn.closed)
conn.close()
示例4: test_init_cookies_with_simple_dict
def test_init_cookies_with_simple_dict(self):
session = ClientSession(
cookies={
"c1": "cookie1",
"c2": "cookie2"
}, loop=self.loop)
self.assertEqual(set(session.cookies), {'c1', 'c2'})
self.assertEqual(session.cookies['c1'].value, 'cookie1')
self.assertEqual(session.cookies['c2'].value, 'cookie2')
session.close()
示例5: test_init_headers_simple_dict
def test_init_headers_simple_dict(self):
session = ClientSession(
headers={
"h1": "header1",
"h2": "header2"
}, loop=self.loop)
self.assertEqual(
sorted(session._default_headers.items()),
([("H1", "header1"),
("H2", "header2")]))
session.close()
示例6: test_merge_headers_with_multi_dict
def test_merge_headers_with_multi_dict(self):
session = ClientSession(
headers={
"h1": "header1",
"h2": "header2"
}, loop=self.loop)
headers = session._prepare_headers(MultiDict([("h1", "h1")]))
self.assertIsInstance(headers, CIMultiDict)
self.assertEqual(headers, CIMultiDict([
("h1", "h1"),
("h2", "header2")
]))
示例7: test_init_headers_list_of_tuples
def test_init_headers_list_of_tuples(self):
session = ClientSession(
headers=[("h1", "header1"),
("h2", "header2"),
("h3", "header3")],
loop=self.loop)
self.assertEqual(
session._default_headers,
CIMultiDict([("h1", "header1"),
("h2", "header2"),
("h3", "header3")]))
session.close()
示例8: test_init_headers_MultiDict
def test_init_headers_MultiDict(self):
session = ClientSession(
headers=MultiDict(
[("h1", "header1"),
("h2", "header2"),
("h3", "header3")]),
loop=self.loop)
self.assertEqual(
session._default_headers,
CIMultiDict([("H1", "header1"),
("H2", "header2"),
("H3", "header3")]))
session.close()
示例9: test_merge_headers_with_list_of_tuples_duplicated_names
def test_merge_headers_with_list_of_tuples_duplicated_names(self):
session = ClientSession(
headers={
"h1": "header1",
"h2": "header2"
}, loop=self.loop)
headers = session._prepare_headers([("h1", "v1"),
("h1", "v2")])
self.assertIsInstance(headers, CIMultiDict)
self.assertEqual(headers, CIMultiDict([
("H2", "header2"),
("H1", "v1"),
("H1", "v2"),
]))
session.close()
示例10: test_merge_headers
def test_merge_headers(self):
# Check incoming simple dict
session = ClientSession(
headers={
"h1": "header1",
"h2": "header2"
}, loop=self.loop)
headers = session._prepare_headers({
"h1": "h1"
})
self.assertIsInstance(headers, CIMultiDict)
self.assertEqual(headers, CIMultiDict([
("h1", "h1"),
("h2", "header2")
]))
示例11: go
def go():
err = OSError(1, "permission error")
req = mock.Mock()
req_factory = mock.Mock(return_value=req)
req.send = mock.Mock(side_effect=err)
session = ClientSession(loop=self.loop, request_class=req_factory)
@asyncio.coroutine
def create_connection(req):
# return self.transport, self.protocol
return mock.Mock(), mock.Mock()
session._connector._create_connection = create_connection
with self.assertRaises(aiohttp.ClientOSError) as ctx:
yield from session.request('get', 'http://example.com')
e = ctx.exception
self.assertEqual(e.errno, err.errno)
self.assertEqual(e.strerror, err.strerror)
示例12: __init__
def __init__(self, rest_service_name='GenericService', spec=None, plugins=None, config=None,
parser=None, serializer=None, base_path='', loop=None, logger=None):
self._plugins = []
self.logger = logger or logging.getLogger('serviceClient.{}'.format(rest_service_name))
self.rest_service_name = rest_service_name
self.spec = spec or {}
self.add_plugins(plugins or [])
self.config = config or {}
self.parser = parser or (lambda x, *args, **kwargs: x)
self.serializer = serializer or (lambda x, *args, **kwargs: x)
self.base_path = base_path
self.loop = loop or get_event_loop()
self.connector = TCPConnector(loop=self.loop, **self.config.get('connector', {}))
self.session = ClientSession(connector=self.connector, loop=self.loop)
示例13: fetch
async def fetch(self, request: Request, session: ClientSession):
if self.use_proxy:
# method, url, *,
# params = None,
# data = None,
# json = None,
# headers = None,
# skip_auto_headers = None,
# auth = None,
# allow_redirects = True,
# max_redirects = 10,
# compress = None,
# chunked = None,
pass
try:
biu = session.request(request.method, request.url,
headers=request.header,
data=request.body)
async with biu as req:
text = await req.text()
return Response(request, text, req.status)
except Exception:
pass
示例14: test_close
def test_close(self):
session = ClientSession(loop=self.loop)
session._connector = mock.Mock(BaseConnector)
session.close()
session._connector.close.assert_called_once_with()
示例15: test_borrow_connector_loop
def test_borrow_connector_loop(connector, create_session, loop):
session = ClientSession(connector=connector, loop=None)
try:
assert session._loop, loop
finally:
session.close()