客户端发起异步请求给aiohttp 服务端。
async def send_post_image_data(img_data, api_url,image_id):
"""
:param img_data: 图片二进制数据
:param api_url: 图片检测接口
:param image_id: 图片id
:return: 返回异步http结果
"""
files = {
'img': base64.b64encode(img_data),
'id': image_id.encode()
}
async with ClientSession() as session:
async with session.post(api_url, data=files) as response:
return await response.text()
发生错误:
Maximum request body size 1048576 exceeded, actual body size 1053619
原因分析:aiohttp异步框架默认post 接收大小是1M
class aiohttp.web.Application(*, logger= , router=None, middlewares=(), handler_args=None, client_max_size=1024**2, loop=None, debug=...)
该类继承于dict。
参数:
logger - logging.Logger实例对象,用于存储应用程序的日志。默认值为logging.getLogger("aiohttp.web")
router - aiohttp.abc.AbstractRouter实例对象,如果是None则默认创建UrlDispatcher。
middlewares - 存放中间件工厂的列表,请看Middlewares一节获取详细信息。
handler_args - 类字典对象,用于覆盖Application.make_handler()中的关键字参数。
client_max_size - 客户端请求中携带的数据的最大大小。如果POST请求超过这个值,将会抛出HTTPRequestEntityTooLarge异常。
loop - 事件循环。自2.0版本后不再赞成使用:在冻结阶段Loop会被自动设置。
debug - 调试组件。
解决方式:设置参数client_max_size为4M
aiohttp.web.Application(client_max_size=1024**2*4)