blob: 145b470d4306dc3150563b896fc2f723c80df593 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
from typing import Optional
from aiohttp import ClientSession
async def get_text(url: str, headers: Optional[dict] = None) -> str:
"""异步以 Get 方式请求 url"""
async with ClientSession() as session:
async with session.get(url, headers=headers) as r:
result = await r.text()
return result
async def get_bytes(url: str, headers: Optional[dict] = None) -> bytes:
"""异步以 Get 方式请求 url"""
async with ClientSession() as session:
async with session.get(url, headers=headers) as r:
result = await r.read()
return result
async def get_content(url: str, headers: Optional[dict] = None):
async with ClientSession() as session:
async with session.get(url, headers=headers) as r:
result = await r.content.read()
return result
async def post_bytes(url: str,
params: Optional[dict] = None,
json: Optional[dict] = None) -> bytes:
"""异步以 Post 方式请求 url"""
async with ClientSession() as session:
async with session.post(url, params=params, json=json) as r:
result = await r.read()
return result
|