Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

websocket api python on close

import asyncio
import datetime
import random
import websockets

async def time(websocket, path):
    while True:
        now = datetime.datetime.utcnow().isoformat() + "Z"
        try:
            await websocket.send(now)
        except websockets.exceptions.ConnectionClosed:
            print("Client disconnected.  Do cleanup")
            break             
        await asyncio.sleep(random.random() * 3)

start_server = websockets.serve(time, "127.0.0.1", 5678)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Comment

PREVIOUS NEXT
Code Example
Python :: python math 
Python :: call shell script from python 
Python :: using csv module how to read perticular lines in csv 
Python :: how to get the end of a item in a python array 
Python :: stock market python 
Python :: async asyncio input 
Python :: numpy shape 
Python :: django get all model fields 
Python :: override get_queryset django with url parameters 
Python :: get element from string with deliminator python 
Python :: python selenium click on agree button 
Python :: arma-garch model python 
Python :: closure python 
Python :: ipython play audio 
Python :: Dictionary Cache 
Python :: generative art python 
Python :: pandas merge sort columns 
Python :: tail a log file with python 
Python :: rotate array in python 
Python :: newsapi 
Python :: Uninstalling/removing a package is very easy with pip: 
Python :: python loops 
Python :: true and false in python 
Python :: python how to iterate through a list of lists 
Python :: read one column pandas 
Python :: get table wikipedia 
Python :: micropython wifi 
Python :: python how to make boxplots with jitter 
Python :: # /usr/bin/env python windows 
Python :: django swagger 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =