zoukankan      html  css  js  c++  java
  • 2.编写Web App骨架——Python实战

    阅读目录:

      1.开始编写

    1.开始编写

    由于我们的Web App建立在 asyncio 的基础上,因此用 aiohttp 写一个基本的 app.py ,以下是廖雪峰老师教程中的代码,但是存在很多问题,

    在web.Response(body=b’<h1>Awesome</h1>’) 中未设置转码和头信息,导致最终点击 http://127.0.0.1:9000 是一个二进制的文件下载;
    app.make_handler() 这种方式已经太过于老旧,运行会报警告;
    在aiohttp中使用yield from方式太过于老旧,如果实在要用下面代码方式的话建议改为 async 和await。
    感兴趣的同学可以看看

    廖雪峰老师教程中的代码:

    import logging; logging.basicConfig(level=logging.INFO)
    
    import asyncio, os, json, time
    from datetime import datetime
    
    from aiohttp import web
    
    def index(request):
        return web.Response(body=b'<h1>Awesome</h1>')
    
    @asyncio.coroutine
    def init(loop):
        app = web.Application(loop=loop)
        app.router.add_route('GET', '/', index)
        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
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()
    

    个人:

    import logging
    from aiohttp import web

    logging.basicConfig(level=logging.INFO) #配置logging的基本信息,level=logging.INFO,记录INFO及以上的日志

    def index(request):
    return web.Response(body='<h1>Awesome</h1>'.encode('utf-8'),content_type='text/html')

    def init():
    app = web.Application()
    app.router.add_route('GET','/', index)
    web.run_app(app,host='127.0.0.1',port=8000)
    logging.info('server started at http://127.0.0.1:9001....')

    init()

    这说明我们的Web App骨架已经搭好了,可以进一步往里面添加更多的东西。

  • 相关阅读:
    MHA-Atlas-MySQL高可用(上)
    MySQL数据库企业级应用实践(主从复制)
    MySQL数据库企业级应用实践(多实例源码编译)
    MySQL存储引擎
    MySQL索引与事务
    MySQL数据备份
    MySQL数据库操作
    bzoj 1038: [ZJOI2008]瞭望塔
    bzoj 2451 Uyuw's Concert
    poj 2187 Beauty Contest
  • 原文地址:https://www.cnblogs.com/zhongbokun/p/10512404.html
Copyright © 2011-2022 走看看