#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @File : t2.py @Time : 2019/05/28 03:11:04 @Author : Liuyuqi @Version : 1.0 @Contact : liuyuqi.gov@msn.cn @License : (C)Copyright 2019 @Desc : None ''' import asyncio import logging from datetime import datetime from aiowebsocket.converses import AioWebSocket 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 = 'ws://echo.websocket.org' try: asyncio.get_event_loop().run_until_complete(startup(remote)) except KeyboardInterrupt as exc: logging.info('Quit.')