AIoWebSocket
新一代异步WebSockets解决方案 https://github.com/asyncins/aiowebsocket
主要特性
- 异步性能:由于基于asyncio,AIoWebSocket能够轻松应对大量并发连接,非常适合大规模实时应用。
- 简洁API:它的API设计简洁明了,上手快速。无论是创建WebSocket服务端还是客户端,只需要几行代码就能实现。
- 丰富的错误处理:提供了详细的错误处理机制,可以帮助开发者更好地调试和优化应用程序。
- 自定义中间件支持:允许开发者插入自定义逻辑,实现如认证、日志记录等功能。
- 兼容性:兼容最新版的Python,并且对标准的WebSocket RFC 6455规范有良好的支持。
# pip install aiowebsocket
# 简单示例
import asyncio
import logging
from datetime import datetime
from aiowebsocket.converses import AioWebSocket
import nest_asyncio
nest_asyncio.apply()
# RuntimeError: This event loop is already running
async def startup(uri):
async with AioWebSocket(uri) as aws:
converse = aws.manipulator
message = b'AioWebSocket - Async WebSocket Client'
while True:
await converse.send(message)
print('{time}-Client send: {message}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
mes = await converse.receive()
print('{time}-Client receive: {rec}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
if __name__ == '__main__':
remote = 'wss://echo.websocket.org'
# remote = 'ws://echo.websocket.org'
try:
asyncio.get_event_loop().run_until_complete(startup(remote))
except KeyboardInterrupt as exc:
logging.info('Quit.')
# custom header
import asyncio
import logging
from datetime import datetime
from aiowebsocket.converses import AioWebSocket
import nest_asyncio
nest_asyncio.apply()
# RuntimeError: This event loop is already running
async def startup(uri, header):
async with AioWebSocket(uri, headers=header) as aws:
converse = aws.manipulator
message = b'AioWebSocket - Async WebSocket Client'
while True:
await converse.send(message)
print('{time}-Client send: {message}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
mes = await converse.receive()
print('{time}-Client receive: {rec}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
if __name__ == '__main__':
remote = 'ws://123.207.167.163:9010/ajaxchattest'
header = [
'GET /ajaxchattest HTTP/1.1',
'Connection: Upgrade',
'Host: 123.207.167.163:9010',
'Origin: http://coolaf.com',
'Sec-WebSocket-Key: RmDgZzaqqvC4hGlWBsEmwQ==',
'Sec-WebSocket-Version: 13',
'Upgrade: websocket',
]
try:
asyncio.get_event_loop().run_until_complete(startup(remote, header))
except KeyboardInterrupt as exc:
logging.info('Quit.')
# union header
import asyncio
import logging
from datetime import datetime
from aiowebsocket.converses import AioWebSocket
import nest_asyncio
nest_asyncio.apply()
# RuntimeError: This event loop is already running
async def startup(uri, union_header):
async with AioWebSocket(uri, union_header=union_header) as aws:
converse = aws.manipulator
message = b'AioWebSocket - Async WebSocket Client'
while True:
await converse.send(message)
print('{time}-Client send: {message}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
mes = await converse.receive()
print('{time}-Client receive: {rec}'
.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
if __name__ == '__main__':
remote = 'ws://123.207.167.163:9010/ajaxchattest'
union_header = {'Origin': 'http://coolaf.com'}
try:
asyncio.get_event_loop().run_until_complete(startup(remote, union_header))
except KeyboardInterrupt as exc:
logging.info('Quit.')