Python_Web_App_Day_1:編寫Web App骨架

來源:互聯網
上載者:User

標籤:服務   關閉   oop   and   awesome   log   art   pre   連接埠   

用到知識點:logging、asyncio、aiohttp模組(後續補充知識點)

"""app.py編寫Web App骨架"""import logging; logging.basicConfig(level=logging.INFO)import asynciofrom aiohttp import webasync def index(request):    """響應函數"""    return web.Response(body=b‘<h1>Awesome</h1>‘, content_type=‘text/html‘)async def init(loop):    """Web App伺服器初始化"""    # 製作響應合集    app = web.Application(loop=loop)    # 把響應函數添加到響應函數集合    app.router.add_route(method=‘GET‘, path=‘/‘, handler=index)    # 建立伺服器(串連網址、連接埠,綁定handler)    srv = await loop.create_server(app.make_handler(), ‘127.0.0.1‘, 9000)    logging.info(‘server start at http://127.0.0.1:9000‘)    return srv# 建立事件loop = asyncio.get_event_loop()# 運行loop.run_until_complete(init(loop))# 伺服器不關閉loop.run_forever()

 

Python_Web_App_Day_1:編寫Web App骨架

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.