connect javascript client to python websocket server

前端 未结 1 1946
故里飘歌
故里飘歌 2021-01-14 18:01

I have this working python websocket server:

#!/usr/bin/env python
from socket import *

HOST = \'\'
PORT = 8080
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSoc         


        
相关标签:
1条回答
  • 2021-01-14 18:44

    The Python socket module doesn't have anything to do with websockets.

    WebSocket, like HTTP, is a protocol implemented on top of an underlying transport. What your code does is setup a TCP server listening on port 8080. Websockets are a layer above that, just like HTTP. You can see the browser attempt to handshake with your server but it gives up because your server doesn't respond appropriately.

    The websocket protocol is somewhat complicated and you'll probably want a library to handle it for you. One example is the websockets module:

    #!/usr/bin/env python
    
    import asyncio
    import websockets
    
    async def echo(websocket, path):
        async for message in websocket:
            await websocket.send(message)
    
    asyncio.get_event_loop().run_until_complete(
        websockets.serve(echo, 'localhost', 8765))
    asyncio.get_event_loop().run_forever()
    
    0 讨论(0)
提交回复
热议问题