本文整理汇总了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()
示例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]
示例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()
示例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()
示例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()
示例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()
示例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
示例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())
示例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
示例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()
示例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
示例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')
)
示例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()
示例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()
示例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))