【学习笔记】Python基础-aiohttp
2017-12-26 19:50
453 查看
aiohttp 的初始化函数init()也是一个coroutine,loop.create_server()则利用asyncio创建TCP服务
具体 廖雪峰老师的
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320981492785ba33cc96c524223b2ea4e444077708d000
http://127.0.0.1:8000/hello/王大锤
具体 廖雪峰老师的
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320981492785ba33cc96c524223b2ea4e444077708d000
安装 aiohttp
安装命令: pip install aiohttpD:\PythonProject\sustudy>pip install aiohttp Collecting aiohttp Downloading aiohttp-2.3.6-cp36-cp36m-win_amd64.whl (370kB) 100% |████████████████████████████████| 378kB 701kB/s Collecting yarl>=0.11 (from aiohttp) Downloading yarl-0.16.0-cp36-cp36m-win_amd64.whl (85kB) 100% |████████████████████████████████| 92kB 383kB/s Collecting multidict>=3.0.0 (from aiohttp) Downloading multidict-3.3.2-cp36-cp36m-win_amd64.whl (185kB) 100% |████████████████████████████████| 194kB 175kB/s Collecting async-timeout>=1.2.0 (from aiohttp) Downloading async_timeout-2.0.0-py3-none-any.whl Requirement already satisfied: chardet in c:\programdata\anaconda3\lib\site-packages (from aiohttp) Installing collected packages: multidict, yarl, async-timeout, aiohttp Successfully installed aiohttp-2.3.6 async-timeout-2.0.0 multidict-3.3.2 yarl-0.16.0
运行示例
# main.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Python基础-Web 服务端 aiohttp import asyncio from aiohttp import web async def index(request): await asyncio.sleep(0.5) return web.Response(body = b'<h1>Index</h1>') async def hello(request): await asyncio.sleep(0.5) text = '<h1>hello, %s!</h1>' % request.match_info['name'] return web.Response(body=text.encode('utf-8')) async def init(loop): app = web.Application(loop = loop) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/hello/{name}', hello) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000) print('Server started at http://127.0.0.1:8000...') loop = asyncio.get_event_loop() loop.run_until_complete(init(loop)) loop.run_forever()
运行结果
浏览器输入:http://127.0.0.1:8000/hello/王大锤
<h1>hello, 王大锤!</h1>
相关文章推荐
- python学习笔记(3)--爬虫基础教程1
- python学习笔记(1)基础知识
- Python基础学习笔记之(二)(华工大神)
- 个人学习Python笔记-基础技能
- Python学习笔记1——Python基础
- Python中的异常处理相关语句基础学习笔记
- python基础都程学习笔记一
- 小甲鱼:Python学习笔记001_变量_分支_数据类型_运算符等基础
- 学习笔记☞ python 基础(python3)【五】(模块,面向对象编程-1)
- Python学习笔记——基础语法(上)
- python基础教程学习笔记 第三章 字符串
- Python 基础学习笔记2(List 的 append , extend方法)
- Python学习笔记2_基础知识
- Python学习笔记之基础(一)基本运算
- Python-基础学习笔记(六)
- 【python学习笔记】7:用python实现爬虫-基础
- Python 0基础学习笔记-day1
- Python学习笔记3-基础语法学习
- python基础教程_学习笔记7:条件、循环、其它语句
- Python3学习笔记(1)——基础和数据类型