面向对象的aiohttp

2024-05-17 13:12:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望创建一个异步web服务器来处理大量的请求。一个朋友推荐我使用aiohttp来实现这个。我渴望尽可能多地封装到一个服务器对象中——这可能吗?我怎样才能做到这一点?你知道吗

我正在使用python3.6,在文档中找不到类似的例子。你知道吗

我想实现类似的目标:

from aiohttp import web


class Server(object):
    def __init__(self):
        self.app = web.Application()
        self.app.add_routes(web.RouteTableDef())

    @routes.get('/')
    async def hello(request):
        return web.Response(text="Hello, world")


if __name__ == '__main__':
    server = Server()

澄清一下,我收到的错误是:

NameError: name 'routes' is not defined

是否有实现这种封装的标准方法?

谢谢


Tags: 对象namefrom文档self服务器webapp