Alexandar Narayan Alexandar Narayan - 1 year ago 219
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)


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download