Alexandar Narayan Alexandar Narayan - 8 months ago 56
Python Question

Clarification on python websockets example

From the example found here:

Can someone explain what the parameter 'path' does here? Is it a tuple for the host and port needed by websocket.serve()?

import asyncio
import websockets

async def hello(websocket, path):
name = await websocket.recv()
print("< {}".format(name))

greeting = "Hello {}!".format(name)
await websocket.send(greeting)
print("> {}".format(greeting))

start_server = websockets.serve(hello, 'localhost', 8765)



The documentation for websockets.serve says that its first argument is ws_handler:

ws_handler is the WebSocket handler. It must be a coroutine accepting two arguments: a WebSocketServerProtocol and the request URI.

In function hello, the second argument is not used, but it must accept the argument, because the argument is going to be sent to it by websockets.serve.