Add server.py
This commit is contained in:
parent
dd2cc51c40
commit
c10c3ec280
47
server.py
Normal file
47
server.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from websockets import serve
|
||||||
|
|
||||||
|
clients = {
|
||||||
|
"FD3ZJFHQ7r" : {
|
||||||
|
"doorOpen" : False,
|
||||||
|
"lockdown" : False,
|
||||||
|
"lastOpened" : 1, #unix timestamp
|
||||||
|
"secLevel" : 1,
|
||||||
|
"openedBy" : "user"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async def echo(websocket):
|
||||||
|
print("Connection established")
|
||||||
|
authenticated = False
|
||||||
|
await websocket.send(json.dumps({
|
||||||
|
"type" : "SAuthReq"
|
||||||
|
}))
|
||||||
|
|
||||||
|
async for msg in websocket:
|
||||||
|
msg_json = json.loads(msg)
|
||||||
|
if authenticated == False:
|
||||||
|
if msg_json["type"] == "CAuth":
|
||||||
|
if msg_json["token"] in clients:
|
||||||
|
await websocket.send(json.dumps({
|
||||||
|
"type" : "SAuthStatus",
|
||||||
|
"failed" : False,
|
||||||
|
"reason" : "Authentication Successful"
|
||||||
|
}))
|
||||||
|
authenticated = True
|
||||||
|
else:
|
||||||
|
await websocket.send(json.dumps({
|
||||||
|
"type" : "SAuthStatus",
|
||||||
|
"failed" : True,
|
||||||
|
"reason" : "Invalid token"
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with serve(echo, "localhost", 8765):
|
||||||
|
await asyncio.get_running_loop().create_future()
|
||||||
|
|
||||||
|
asyncio.run(main())
|
Loading…
Reference in New Issue
Block a user