美文网首页
mac 搭建websocket

mac 搭建websocket

作者: sws1314 | 来源:发表于2022-01-18 16:56 被阅读0次

    mac 搭建websocket

    pip3 install websockets
    

    服务端:

    import asyncio
    import websockets
    
    
    async def echo(websocket, path):
        async for message in websocket:
            message = "I got your message: {}".format(message)
            print(message)
            await websocket.send(message)
    
    
    asyncio.get_event_loop().run_until_complete(websockets.serve(echo, '0.0.0.0', 8282))
    asyncio.get_event_loop().run_forever()
    
    

    客户端:

    #!/usr/bin/env python
    import asyncio
    import websockets
    
    async def hello(uri):
        async with websockets.connect(uri) as websocket:
            await websocket.send("hello world")
            print("< HELLO WORLD")
            while True:
                recv_text = await websocket.recv()
                print("> {}".format(recv_text))
    
    asyncio.get_event_loop().run_until_complete(hello('ws://192.168.0.5:8282'))
    
    

    相关文章

      网友评论

          本文标题:mac 搭建websocket

          本文链接:https://www.haomeiwen.com/subject/wfgkhrtx.html