Websockets
import asyncio
import websockets
async def handler(websocket):
while True:
message = await websocket.recv()
await websocket.send(message)
async def main():
async with websockets.serve(handler, "localhost", 8765):
await asyncio.Future() # Run forever
asyncio.run(main())import asyncio
import websockets
import json
async def handler(websocket):
while True:
stock_data = await get_stock_data()
await websocket.send(json.dumps(stock_data))
async def get_stock_data():
# Fetch stock data from an API or other source
async def main():
async with websockets.serve(handler, "localhost", 8765):
await asyncio.Future()
asyncio.run(main())