✍17 aiohttp模块的使用

from aiohttp import web
import socketio

# 创建一个新的aysnc套接字io服务器
sio = socketio.AsyncServer()

#创建一个新的Aiohttp Web应用程序
app = web.Application()

#将socket.io服务器绑定到Web应用程序实例
sio.attach(app)

#定义端点(起始HTML文件) 
async def index(request):
    filename = r"./sockrtio_ve.html"
    with open(filename) as f:
        return web.Response(text = f.read(), content_type='text/html')


@socket_io.on('msg')
async def print_message(id, message):
    print("socket id is {}".format(id))
    print(message)
    #实现双向通讯
    await sio.emit('message', "you said {}".format(message))


#将aiohttp端点绑定到web_application
app.router.add_get('/',index)

#启动服务器
if __name__ == '__main__':
    web.run_app(app)
posted @ 2021-11-25 20:57  给你骨质唱疏松  阅读(10)  评论(0编辑  收藏  举报