reworking the project on django

This commit is contained in:
Yamozha
2021-02-05 00:57:22 +02:00
parent 98f8b51a42
commit 3dfd277b99
8 changed files with 138 additions and 0 deletions

Binary file not shown.

1
old/Server/currency.txt Normal file
View File

@ -0,0 +1 @@
6969

View File

@ -0,0 +1,7 @@
userNames = {"1":"boyan", "2":"nikola"}
usersCoins = {"1":"200", "2":"100"}
def checkBalance(ID):
userName = userNames[f"{ID}"]
userCoins = usersCoins[f"{ID}"]
return userName, userCoins

View File

@ -0,0 +1,39 @@
import asyncio
import websockets
import logging
from ratelimit import limits
import time
from databasefornow import *
logger = logging.getLogger('websockets')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
print("Ready")
# calls in number of requests
# period in seconds
@limits(calls=20, period=5)
async def serveStuff(websocket, path):
requestText = await websocket.recv()
print(requestText)
if requestText == "STATUSONCOIN":
file = open("currency.txt","r")
currencyStatus = file.readline()
file.close()
await websocket.send(currencyStatus)
# bad way to send balance to given user - FIX!!!
elif requestText == "STATUSONUSER":
name, balance = checkBalance("1")
print(name)
await websocket.send(f"Your balance is: {balance}")
await websocket.send(f"Welcome, {name}!")
else:
await websocket.send("We don't accept DoS")
start_server = websockets.serve(serveStuff, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()