Beispiel #1
0
async def init(loop):
    # 创建全局数据库连接池
    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')
    # 创建web应用
    app = web.Application(loop=loop,
                          middlewares=[logger_factory, response_factory])

    #创建一个Application实例
    # 设置模板为jinja2,并用时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册所有url处理函数
    add_routes(app, 'handlers')
    # 将当前目录下的static目录加入app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')  #发送日志
    return srv
Beispiel #2
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')
    # middlewares(中间件)设置3个中间处理函数(都是装饰器)
    # middlewares中的每个factory接受两个参数,app 和 handler(即middlewares中的下一个handler)
    # 譬如这里logger_factory的handler参数其实就是auth_factory
    # middlewares的最后一个元素的handler会通过routes查找到相应的,就是routes注册的对应handler处理函数
    # 这是装饰模式的体现,logger_factory, auth_factory, response_factory都是URL处理函数前(如handler.index)的装饰功能
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 添加URL处理函数
    add_routes(app, 'handlers')
    # 添加CSS等静态文件路径
    add_static(app)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Beispiel #3
0
async def init(loop):

    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')

    app = web.Application(loop=loop,
                          middlewares=[logger_factoty, data_factory])

    init_jinja2(app, filters=dict(datetime=datetime_filter))

    add_routes(app, 'handlers')

    add_static(app)

    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)

    logging.info('server started at http://127.0.0.1:9000...')

    return srv
Beispiel #4
0
async def init(loop):
    await orm.create_pool(loop=loop,
                          host='localhost',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')
    # 获取程序中的EventLoop,初始化,设置路由
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    # app.router.add_route('GET', '/', index)
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 添加URL处理函数, 参数handlers为模块名
    add_routes(app, 'handlers')
    # 添加CSS等静态文件路径
    add_static(app)
    # 启动
    # 因为asyncio是协程所以立马中断去执行下一个循环,然后继续执行下一句。
    # srv是生成器,init,srv只产生一次,每次的连接请求是srv生成的。所以这个函数就是返回一个生成器。
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server 开始于 127.0.0.1')
    return srv
Beispiel #5
0
def init(loop):
    # 创建全局数据库连接池
    yield from orm.create_pool(loop=loop,
                               host='127.0.0.1',
                               port=3306,
                               user='******',
                               password='******',
                               db='mypython')
    #创建web应用
    # 创建一个循环类型是消息循环的web应用对象
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    # 设置模板为jiaja2, 并以时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filer))
    # 注册所有url处理函数
    add_routes(app, 'handlers')
    # 将当前目录下的static目录将如app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000')
    return srv
Beispiel #6
0
async def init(loop):
    # 初始化 connection pool
    await orm.create_pool(loop=loop,
                          host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          database='awesome')

    # 创建web应用,
    app = web.Application(loop=loop,
                          middlewares=[
                              logger_factory, data_factory, response_factory,
                              auth_factory
                          ])

    init_jinja2(app, filters=dict(datetime=datetime_filter))

    add_static(app)
    add_routes(app, "handlers")

    server = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000')
    return server
Beispiel #7
0
def init(loop):
    # 创建全局数据库连接池
    yield from orm.create_pool(loop=loop,
                               host="127.0.0.1",
                               port=3306,
                               user="******",
                               password="******",
                               db="awesome",
                               autocommit=True)
    # 创建web应用,
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory,
                     response_factory])  # 创建一个循环类型是消息循环的web应用对象
    # 设置模板为jiaja2, 并以时间为过滤器
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 注册所有url处理函数
    add_routes(app, "handlers")
    # 将当前目录下的static目录将如app目录
    add_static(app)
    # 调用子协程:创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = yield from loop.create_server(app.make_handler(), "127.0.0.1", 9000)
    logging.info("server started at http://127.0.0.1:9000")
    return srv
Beispiel #8
0
def init(loop):
    yield from orm.create_pool(loop=loop,
                               host='127.0.0.1',
                               port=3306,
                               user='******',
                               password='******',
                               database='excodout')
    # 梗#了解下 middlewares
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])
    init_jinjia2(app, filters=dict(datetime=datetime_filter))
    # add_routes第二个参数是模块名,把模块中所有函数处理后添加给app
    # ipdb.set_trace()
    add_routes(app, 'handlers')
    add_static(app)
    # app.router.add_route('GET', '/', index)
    hostI = ['192.168.0.104', '127.0.0.1']
    hostII = ['192.168.0.106', '127.0.0.1']
    # srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9999)
    srv = yield from loop.create_server(app.make_handler(), hostI, 9999)
    # srv = yield from loop.create_server(app.make_handler(), '192.168.0.106', 9999)
    logger.info('Server started at http://127.0.0.1:9999...')
    return srv
Beispiel #9
0
def init(loop):
    # init connection pool
    yield from orm.create_pool(loop=loop,
                               user='******',
                               port=3306,
                               password='******',
                               db='web')

    # 实例化一个web Application
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])

    init_jiaja2(app, filters=dict(datetime=datetime_filter))

    # 设置请求路径(url地址)及其响应
    add_routes(app, 'handlers')
    add_static(app)

    # 为每一个请求建立一个协程连接
    srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('Server start at http://127.0.0.1:9000...')
    print('Server start at http://127.0.0.1:9000...\n\n\n')
    return srv
Beispiel #10
0
async def init(loop):
    await orm.create_pool(loop=loop, host='127.0.0.1', port=3306, user='******', password='******', db='awesome')
    """ 
    To get fully working example, you have to 
    (1)make application
    (2)register supported urls in router
    (3)create a server socket with Server as a protocol factory
    """
    # 创建web应用
    app = web.Application(loop=loop, middlewares=[logger_factory, auth_factory, response_factory])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    # 将处理函数与对应的URL绑定,注册到创建的app.router中
    # 此处把通过GET方式传过来的对根目录的请求转发给index函数处理
    # app.router.add_route('GET', '/', index)
    add_routes(app, 'handlers')
    add_static(app)
    # 用aiohttp.RequestHandlerFactory作为协议簇创建套接字,用make_handle()创建,用来处理HTTP协议
    # yield from 返回一个创建好的,绑定IP、端口、HTTP协议簇的监听服务的协程 
    # 此处调用协程创建一个TCP服务器,绑定到"127.0.0.1:9000"socket,并返回一个服务器对象
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')

    # await orm.destroy_pool()
    return srv
Beispiel #11
0
async def init(loop):
    await orm.create_pool(loop=loop, **configs.db)
    app = web.Application(loop=loop,
                          middlewares=[
                              logger_factory, auth_factory, data_factory,
                              response_factory
                          ])
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'core.handlers.handler')
    add_routes(app, 'core.handlers.users_handler')
    add_routes(app, 'core.handlers.blogs_handler')
    add_routes(app, 'core.handlers.comments_handler')
    add_static(app)
    srv = await loop.create_server(app.make_handler(), configs.server.host,
                                   configs.server.port)
    logging.info('app server started at http://%s:%s...' %
                 (configs.server.host, configs.server.port))
    return srv
async def init():
    await orm.create_pool(host='127.0.0.1',
                          port=3306,
                          user='******',
                          password='******',
                          db='awesome')
    app = web.Application(
        middlewares=[logger_factory, auth_factory, response_factory])
    # app.router.add_route('GET', '/', index)
    init_jinja2(app, filters=dict(datetime=datetime_filter))
    add_routes(app, 'handlers')
    add_routes(app, 'api_handlers')
    add_routes(app, 'dao.handlers')
    add_static(app)
    #add_vue_static(app)
    app_runner = web.AppRunner(app)
    await app_runner.setup()
    srv = await loop.create_server(app_runner.server, '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv
Beispiel #13
0
            'Access-Control-Allow-Headers'] = "Origin, X-Requested-With, Content-Type, Accept"
        resp.headers[
            'Access-Control-Allow-Methods'] = "PUT,POST,GET,DELETE,OPTIONS"
        # resp.headers['Access-Control-Max-Age'] = '1000'
        # resp.headers['Access-Control-Allow-Headers'] = '*'
        return resp

    return response


async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    r = web.Response(text=text)
    r.headers['Access-Control-Allow-Origin'] = "*"
    r.headers[
        'Access-Control-Allow-Headers'] = "Origin, X-Requested-With, Content-Type, Accept"

    return r


app = web.Application(middlewares=[logger_factory, response_factory])

add_routes(app, 'handlers')

# app.add_routes([web.post('/logIn', handle),
# web.options('/logIn', handle),
#                 web.get('/logIn', handle)])

if __name__ == '__main__':
    web.run_app(app)
Beispiel #14
0
def main():
    loop = asyncio.get_event_loop()
    app = loop.run_until_complete(init())
    add_routes(app, 'handlers')
    add_static(app)
    web.run_app(app, host=configs.web.host, port=configs.web.port)
Beispiel #15
0
def init(loop):
    app = web.Application(
        loop=loop,
        middlewares=[logger_factory, auth_factory, response_factory])

    add_routes(app, 'apiuser')
    add_routes(app, 'apiarray')
    add_routes(app, 'apidisk')
    add_routes(app, 'apivg')
    add_routes(app, 'apilvm')
    add_routes(app, 'apitarget')
    add_routes(app, 'apisystem')
    add_routes(app, 'apievent')
    srv = yield from loop.create_server(app.make_handler(),
                                        configs.host.address,
                                        configs.host.port)

    logging.info('Server start at port %s' % configs.host.port)
    return srv
async def init(loop):
    app = web.Application(loop=loop, middlewares=[response_factory])
    add_routes(app, 'coroweb_test')
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv