我正在从requests
迁移到aiohttp
,并且以前使用requests.Session.params
为会话中的每个请求设置默认查询参数。
我如何正确地使用aiohttp.ClientSession
达到等效的水平?
我尝试过:
aiohttp.ClientSession
以覆盖_request
方法。
但是会产生DepcrecationWarning: Inheritance from ClientSession is discouraged
。aiohttp.ClientResponse
进行子类化以覆盖__init__
方法,并在那里更新params
,然后将子类传递给aiohttp.ClientSession
。
那将行不通,因为我需要基于会话定义(并且可变)自定义参数。编辑:我希望代码看起来更清晰的示例:
from aiohttp import ClientSession
session = ClientSession()
# Do something fancy here, or in the initialization of `ClientSession`, so that
# `default_params` is set as the default parameters for each request
default_params = dict(some_parameter="a value")
async with session.get("http://httpbin.org/get") as resp:
j = await resp.json()
assert j["args"] == default_params
# Do something fancy here, where we change the default parameters
default_params.update(some_parameter="another value")
async with session.get("http://httpbin.org/get") as resp:
j = await resp.json()
assert j["args"] == default_params
答案 0 :(得分:2)
自定义请求标题
如果您需要将HTTP标头添加到请求中,则将它们以字典形式传递给headers参数。
它在文档http://docs.aiohttp.org/en/stable/client_advanced.html#custom-request-headers
中url = 'http://example.com/image'
payload = b'GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00'
b'\x00\x00\x01\x00\x01\x00\x00\x02\x00;'
headers = {'content-type': 'image/gif'}
await session.post(url,
data=payload,
headers=headers)
要添加默认值和以后的自定义参数,您可以像这样进行操作而无需进行任何子类化(based on this part of documentation)
import asyncio
from aiohttp import ClientSession
async def fetch(url, params, loop):
async with ClientSession() as session:
async with session.get(url, params=params) as response:
print(response.request_info)
print(str(response.url))
print(response.status)
args = await response.json()
resp_text = await response.text()
print('args:', args['args'])
print(resp_text)
def main(url, params):
loop = asyncio.get_event_loop()
return loop.run_until_complete(fetch(url, params, loop))
if __name__ == '__main__':
url = 'http://httpbin.org/get'
default_params = {'param1': 1, 'param2': 2}
main(url, default_params)
new_params = {'some_parameter': 'a value', 'other_parameter': 'another value'}
default_params.update(new_params)
main(url, default_params)
结果:
RequestInfo(url=URL('http://httpbin.org/get?param1=1¶m2=2'), method='GET', headers=<CIMultiDict('Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'Python/3.5 aiohttp/3.4.4')>, real_url=URL('http://httpbin.org/get?param1=1¶m2=2'))
http://httpbin.org/get?param1=1¶m2=2
200
args: {'param1': '1', 'param2': '2'}
{
"args": {
"param1": "1",
"param2": "2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Python/3.5 aiohttp/3.4.4"
},
"origin": "10.10.10.10",
"url": "http://httpbin.org/get?param1=1¶m2=2"
}
RequestInfo(url=URL('http://httpbin.org/get?param1=1¶m2=2&some_parameter=a+value&other_parameter=another+value'), method='GET', headers=<CIMultiDict('Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'Python/3.5 aiohttp/3.4.4')>, real_url=URL('http://httpbin.org/get?param1=1¶m2=2&some_parameter=a+value&other_parameter=another+value'))
http://httpbin.org/get?param1=1¶m2=2&some_parameter=a+value&other_parameter=another+value
200
args: {'other_parameter': 'another value', 'param1': '1', 'param2': '2', 'some_parameter': 'a value'}
{
"args": {
"other_parameter": "another value",
"param1": "1",
"param2": "2",
"some_parameter": "a value"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Python/3.5 aiohttp/3.4.4"
},
"origin": "10.10.10.10",
"url": "http://httpbin.org/get?param1=1¶m2=2&some_parameter=a+value&other_parameter=another+value"
}
答案 1 :(得分:1)
ClientSession
接受request_class
作为其参数之一。它应该是aiohttp.client_reqrep.ClientRequest
的实例。您可以从中继承并设置默认值,但这不是很动态。另一种选择是创建一个返回ClientRequest
的包装函数,并在其中添加默认值的逻辑。因为ClientSession
只是呼叫req = self._request_class(
here
示例 Python 3.8
import asyncio
import aiohttp
import aiohttp.client_reqrep
def wrap(default_params):
def request_class(*args, **kwargs):
"""
Merge defaults and pass them along to the ClientRequest class
"""
kwargs["params"] = {**default_params, **params} \
if (params := kwargs.get("params")) else default_params
return aiohttp.client_reqrep.ClientRequest(*args, **kwargs)
return request_class
async def run():
default_params = dict(some_parameter="a value")
async with aiohttp.ClientSession(request_class=wrap(default_params)) as session:
async with session.get("http://httpbin.org/get") as resp:
j = await resp.json()
print(j)
assert j["args"] == default_params
# Do something fancy here, where we change the default parameters
default_params.update(some_parameter="another value")
async with session.get("http://httpbin.org/get") as resp:
j = await resp.json()
print(j)
assert j["args"] == default_params
async with session.get("http://httpbin.org/get",
params={"foo": "bar"}) as resp:
j = await resp.json()
print(j)
assert j["args"] == {**{"foo": "bar"}, **default_params}
async with session.get("http://httpbin.org/get",
params={"some_parameter": "foo"}) as resp:
j = await resp.json()
print(j)
assert j["args"] == {"some_parameter": "foo"}
# run
asyncio.run(run())