參考鏈接: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'<h1>Index</h1>'
;
/hello/{name}
- 根據URL參數返回文本hello, %s!
。
代碼:
import asyncio from aiohttp import web async def hello(request):#創建請求處理程序 await asyncio.sleep(0.5) text='<h1>hello ,%s!</h1>' % 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='<h1>Index</h1>'.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='<h1>hello ,%s!</h1>' % request.match_info['name'] #return web.Response(body=text.encode('utf-8'))#以特定編碼返回要 return web.Response(body=text.encode(),content_type='text/html')