Files
delugeClient/deluge_socket.py
2018-08-12 23:17:40 +02:00

38 lines
895 B
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import datetime
import random
import websockets
import json
import deluge_cli
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}, welcome to the world of websockets!"
await websocket.send(greeting)
print(f"> {greeting}")
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
await asyncio.sleep(1)
async def deluge(websocket, path):
while True:
downloading = deluge_cli.main(['progress'])
await websocket.send(json.dumps(downloading))
await asyncio.sleep(1)
serve_hello = websockets.serve(hello, '0.0.0.0', 8765)
serve_deluge = websockets.serve(deluge, '0.0.0.0', 5678)
asyncio.get_event_loop().run_until_complete(serve_hello)
asyncio.get_event_loop().run_until_complete(serve_deluge)
asyncio.get_event_loop().run_forever()