zoukankan      html  css  js  c++  java
  • aiohttp AppRunner的用法

    参考廖雪峰的aiohttp教程,会出现两个DeprecationWarning,

    • loop argument is deprecated
    • Application.make_handler(...) is deprecated, use AppRunner API instead

    解决方案

    • loop 参数直接不用就是
    • 参见aiohttp官网,AppRunner的用法:
      The simple startup code for serving HTTP site on 'localhost', port 8080 looks like:
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, 'localhost', 8080)
    await site.start()
    
    # -*- coding: utf-8 -*-
    import logging
    logging.basicConfig(level=logging.INFO)
    import os,json,time,asyncio
    from datetime import datetime
    from aiohttp import web
    
    # def index(request): # 原始简单的url处理函数
    #     return web.Response(body=b'<h1>Awesome</h1>',content_type='text/html')
    
    def init_jinja2(app, **kw): #初始化 jinja2的 env
       pass
    
    async def logger_factory(app, handler):
        async def logger(request):
            logging.info('Request: %s %s' % (request.method, request.path))
            return (await handler(request))
        return logger
    
    #  生产  post 提交的数据
    async def data_factory(app, handler):
       pass
    
    #将url处理函数的返回值 转换成 response 对象
    async def response_factory(app, handler):
        pass
        return response
    
    #  将blog  评论的发布时间 转换成 多少时间以前
    def datetime_filter(t):
        delta = int(time.time() - t)
        if delta < 60:
            return u'1分钟前'
        if delta < 3600:
            return u'%s分钟前' % (delta // 60)
        if delta < 86400:
            return u'%s小时前' % (delta // 3600)
        if delta < 604800:
            return u'%s天前' % (delta // 86400)
        dt = datetime.fromtimestamp(t)
        return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)
    
    async def init(loop):
        db = configs.configs.db
        await orm.create_pool(loop=loop, **db)
        #DeprecationWarning: loop argument is deprecated
        app = web.Application(loop = loop,middlewares=[ #拦截器 一个URL在被某个函数处理前,可以经过一系列的middleware的处理。
            logger_factory, response_factory #工厂模式
        ])
        init_jinja2(app, filters=dict(datetime=datetime_filter))
        add_routes(app, 'handlers')
        add_static(app)
    
        # DeprecationWarning: Application.make_handler(...) is deprecated, use AppRunner API instead
        runner = web.AppRunner(app)
        await runner.setup()
        site = web.TCPSite(runner, '192.168.2.101', 9000)
        logging.info('server started at http://192.168.2.101:9000...')
        await site.start()
         
        #以前的写法
        # srv = await loop.create_server(app.make_handler(), '192.168.2.101', 9000)
        # logging.info('server started at http://192.168.2.101:9000...')
        # return srv
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()
    
    
  • 相关阅读:
    CSU 1598 最长公共前缀 (简单KMP或者暴力)
    CSU
    HDU 1711 Number Sequence (KMP简单题)
    HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
    HDU 1116 Play on Words(欧拉回路+并查集)
    POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)
    HDU 1165 Eddy's research II(给出递归公式,然后找规律)
    使用kvm虚拟出Centos6.5系统相关步骤
    netstat命令
    Centos6.5安装和使用docker
  • 原文地址:https://www.cnblogs.com/ShawSpring/p/10659226.html
Copyright © 2011-2022 走看看