python 高并发http客户端_python 异步IO-aiohttp与简单的异步HTTP客户端/服务器

参考链接: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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值