参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320981492785ba33cc96c524223b2ea4e444077708d000
asyncio和Python的异步HTTP客户端/服务器:https://docs.aiohttp.org/en/latest/web_quickstart.html
aiohttp 是基于 asynico 的http框架,由于 asyncio 实现了单线程并发IO操作。如果仅用在客户端,发挥的用处不大。而由于http就是IO操作,所以可以用在服务端。就可以用单线程 +coroutine 实现单线程多用户的高并发支持。
asyncio 实现了TCP、UDP、SSL等协议。 aiohttp 就是基于 asyncio 实现的http框架
接下来作者举了一个例子,来演示了实现多用户高并发的功能:
安装aiohttp
创建一个服务器,处理两个url:
/ - 首页返回b'
Index
';/hello/{name} - 根据URL参数返回文本hello, %s!。
代码:
import asyncio
from aiohttp import web
async def hello(request):#创建请求处理程序
await asyncio.sleep(0.5)
text='
hello ,%s!
' % request.match_info['name'] #这里的name是在init()里面注册的url里确定的#return web.Response(body=text.encode('utf-8'))#以特定编码返回要
return web.Response(body=text.encode(),content_type='text/html')
async def index(request):
return web.Response(body='
Index
'.encode(), content_type='text/html')async def init(loop):
app = web.Application()#创建application实例
app.router.add_route('GET','/', index)#注册路径与请求处理程序
app.router.add_route('GET','/hello/{name}',hello)#之所以上面能识别name,就是因为在这里定义的。
srv = await loop.create_server(app._make_handler(),'127.0.0.1', 9000)
print('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()
错误
1)我按照老师给的代码运行时,却出现了错误
(web_go) λ python Envs\forTest.py
server started at http://127.0.0.1:9000...
Error handling request
Traceback (most recent call last):
File "C:\Users\Administrator.SC-201605202132\Envs\web_go\lib\site-packages\aiohttp\web_protocol.py", line 275,
in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method
Error handling request
Traceback (most recent call last):
File "C:\Users\Administrator.SC-201605202132\Envs\web_go\lib\site-packages\aiohttp\web_protocol.py", line 275,
in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method
Error handling request
Traceback (most recent call last):
File "C:\Users\Administrator.SC-201605202132\Envs\web_go\lib\site-packages\aiohttp\web_protocol.py", line 275,
in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp\_http_parser.pyx", line 523, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: invalid HTTP method
这个错误我没有解决,来回改了几次之后,发现这个错误也没有了,也不知道是为啥
2)出现了命令行无响应的情况,后来发现使用Ctrl+C没有用,必须在关闭后在刷新一下URL才可以的。
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320981492785ba33cc96c524223b2ea4e444077708d000
3)当请求URL时,响应的文本不会当成HTML解析,而是会作为文件自动下载出来。
解决方法:
async def hello(request):#创建请求处理程序
await asyncio.sleep(0.5)
text='
hello ,%s!
' % request.match_info['name']#return web.Response(body=text.encode('utf-8'))#以特定编码返回要
return web.Response(body=text.encode(),content_type='text/html')