import asyncio
from aiohttp import web
import json
import random
import pprint

pp = pprint.PrettyPrinter(indent=4)

import networkstats

wsclients = []
lnetworkstats = {
    "dscps": [0, 46],
    "eth1": {
        "rx":[100],
        "tx":[100],
    },
    "eth2":{
        "rx":[100],
        "tx":[100]
    }
}

async def handle(request):
    indexpage = None

    with open('index.html', 'r') as indexfile:
        indexpage=indexfile.read()
    return web.Response(text=indexpage, content_type='text/html')

async def wshandler(request):
    ws = web.WebSocketResponse(protocols=['SUPERNET'])
    await ws.prepare(request)

    wsclients.append(ws)
   
    #send an initial response
    async for msg in ws:
        if msg.type == web.MsgType.text:
            ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.MsgType.binary:
            ws.send_bytes(msg.data)
        elif msg.type == web.MsgType.close:
            break
    return ws

def readstats(networkstats):
    networkstats["eth1"]["rx"].append(random.randint(0, 255))
    networkstats["eth1"]["tx"].append(random.randint(0, 255))

    networkstats["eth2"]["rx"].append(random.randint(0, 255))
    networkstats["eth2"]["tx"].append(random.randint(0, 255))

    return networkstats 

async def updatestats(app):
    global lnetworkstats
    while True:
        await asyncio.sleep(1)

        lnetworkstats = readstats(lnetworkstats)

        for client in wsclients:
            #client.send_str(json.dumps("{'key':'value'}"))
            #client.send_str(json.dumps(lnetworkstats))
            #print("sending to ws")
            #pp.pprint(networkstats.interfaces)
            client.send_str(json.dumps(networkstats.interfaces))

async def start_background_tasks(app):
    app.loop.create_task(updatestats(app))

app = web.Application()

app.router.add_static('/d3', "d3")
app.router.add_static('/css', "css")
app.router.add_static('/js', "js")

app.router.add_get('/ifstatus', wshandler)
app.router.add_get('/', handle)

app.on_startup.append(start_background_tasks)
app.on_startup.append(networkstats.start_monitoring)

web.run_app(app)

import asyncio
from aiohttp import web
import json
import random
import pprint

pp = pprint.PrettyPrinter(indent=4)

import networkstats

wsclients = []

async def handle(request):
    indexpage = None

    with open('index.html', 'r') as indexfile:
        indexpage=indexfile.read()
    return web.Response(text=indexpage, content_type='text/html')

async def wshandler(request):
    ws = web.WebSocketResponse(protocols=['SUPERNET'])
    await ws.prepare(request)

    wsclients.append(ws)
   
    #send an initial response
    async for msg in ws:
        if msg.type == web.MsgType.text:
            ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.MsgType.binary:
            ws.send_bytes(msg.data)
        elif msg.type == web.MsgType.close:
            break
    return ws

def readstats(networkstats):
    networkstats["eth1"]["rx"].append(random.randint(0, 255))
    networkstats["eth1"]["tx"].append(random.randint(0, 255))

    networkstats["eth2"]["rx"].append(random.randint(0, 255))
    networkstats["eth2"]["tx"].append(random.randint(0, 255))

    return networkstats 

async def updatestats(app):
    while True:
        await asyncio.sleep(1)
        for client in wsclients:
            client.send_str(json.dumps(networkstats.interfaces))

async def start_background_tasks(app):
    app.loop.create_task(updatestats(app))

app = web.Application()

app.router.add_static('/d3', "d3")
app.router.add_static('/css', "css")
app.router.add_static('/js', "js")

app.router.add_get('/ifstatus', wshandler)
app.router.add_get('/', handle)

app.on_startup.append(start_background_tasks)
app.on_startup.append(networkstats.start_monitoring)

web.run_app(app)

Tidy up old declarations