在python 3 asyncio上使用hyper-h2实现http/2。
aioh2的Python项目详细描述
空气2
在python 3异步上使用hyper-h2实现http/2。
- 免费软件:BSD许可证
- 文档:https://aioh2.readthedocs.org。
功能
- 异步http/2客户端和服务器
- 数据流与托管流和优先级控制< /LI>
- 可选无痒健康检查
- 更多信息
非功能:
- 请求/响应包装器
- Web服务器、调度器、cookie等
- http/2升级
示例
一个打招呼的服务器:
# Server request handlerasyncdefon_connected(proto):try:whileTrue:# Receive a request from queuestream_id,headers=awaitproto.recv_request()# Send response headersawaitproto.start_response(stream_id,{':status':'200'})# Response body starts with "hello"awaitproto.send_data(stream_id,b'hello, ')# Read all request body as a name from client sidename=awaitproto.read_stream(stream_id,-1)# Amend response body with the nameawaitproto.send_data(stream_id,name)# Send trailers with the length of the nameawaitproto.send_trailers(stream_id,{'len':str(len(name))})exceptasyncio.CancelledError:# do cleanup herepass# Start server on random port, with maximum concurrent requests of 3server=awaitaioh2.start_server(on_connected,port=0,concurrency=3)port=server.sockets[0].getsockname()[1]
还有一个客户要试用:
# Open client connectionclient=awaitaioh2.open_connection('0.0.0.0',port,functional_timeout=0.1)# Optionally wait for an ack of tickless ping - a.k.a. until functionalawaitasyncio.sleep(0.1)# simulate client being busy with something elsertt=awaitclient.wait_functional()ifrtt:print('Round-trip time: %.1fms'%(rtt*1000))# Start request with headersstream_id=awaitclient.start_request({':method':'GET',':path':'/index.html'})# Send my name "world" as whole request bodyawaitclient.send_data(stream_id,b'world',end_stream=True)# Receive response headersheaders=awaitclient.recv_response(stream_id)print('Response headers:',headers)# Read all response bodyresp=awaitclient.read_stream(stream_id,-1)print('Response body:',resp)# Read response trailerstrailers=awaitclient.recv_trailers(stream_id)print('Response trailers:',trailers)client.close_connection()awaitasyncio.sleep(.1)
上面的例子可以在examples/core.py找到。
学分
非常感谢来自Cory Benfield的伟大图书馆hyper-h2。
DecentFoX Studio是一家提供高质量服务的软件外包公司 为全球客户提供基于网络的产品和移动应用,采用敏捷方法, 专注于前沿技术和快速开发的可扩展架构。
这个包是用Cookiecutter和audreyr/cookiecutter-pypackage项目模板创建的。
历史记录
0.2.1(2017-12-03)
- 更新至H23.0(由Arthur Darcet和Peter Wu提供)
- 放弃了对Python3.3的支持
0.1.0(2016-2-6)
- pypi上的第一个版本。