-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (122 loc) · 3.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
import asyncio
import sqlite3
import json
from websockets.asyncio.server import serve
'''
input
{
"amount": <int>
}
'''
async def set_in_db(amount):
with sqlite3.connect('example.db') as connection:
# Create a cursor object to interact with the database
cursor = connection.cursor()
# 3. Create a Table
# Let's create a simple table named 'users' with three columns: id, name, and age
cursor.execute('''
CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY,
amount INTEGER NOT NULL
)
''')
# Commit the changes to the database (this saves the changes)
connection.commit()
# 4. Insert Data
cursor.execute('''
REPLACE INTO requests (id, amount)
VALUES (1, ?)
''', (amount,))
# Commit the changes to the database
connection.commit()
# # 5. Query Data
# # Query the data from the 'users' table to ensure it was inserted correctly
cursor.execute('SELECT * FROM requests ORDER BY id DESC LIMIT 1')
row = cursor.fetchone() # Fetch all rows from the executed query
print(row)
# return id
return[row][0][0]
async def access_db(id):
print("hih")
with sqlite3.connect('example.db') as connection:
print("accessing db")
# Create a cursor object to interact with the database
cursor = connection.cursor()
# 4. Insert Data
cursor.execute('''
SELECT amount FROM requests WHERE id = ?
''', (id,))
# # 5. Query Data
# # Query the data from the 'users' table to ensure it was inserted correctly
# cursor.execute('SELECT * FROM users')
rows = cursor.fetchall() # Fetch all rows from the executed query
print("accessed row")
print(rows)
ret_val = None
if len(rows) == 0:
return None
else:
ret_val = rows[-1][0]
cursor.execute('''
DELETE FROM requests WHERE id = 1
''')
connection.commit()
return ret_val
'''
input:
{
"amount": <int>
}
'''
async def set_amount(websocket):
req = await websocket.recv()
print(f"set_amount recieved {req}")
# parse the req json object
req_json = {}
try:
req_json = json.loads(req)
print(f"Received JSON object: {req_json}")
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
await websocket.send("error")
return
id = await set_in_db(req_json["amount"])
await websocket.send(f"{id}")
print(f"done")
'''
input:
{
"id": <int>
}
'''
async def get_amount(websocket):
print(f"get_amount recieved")
req = await websocket.recv()
# parse the req json object
req_json = {}
try:
req_json = json.loads(req)
print(f"Received JSON object: {req_json}")
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
await websocket.send("error")
return
amount = await access_db(req_json["id"])
if amount == None:
await websocket.send("")
return
await websocket.send(f"{amount}")
print(f"done")
async def handler(websocket):
if websocket.request.path == "/set_amount":
await set_amount(websocket)
elif websocket.request.path == "/get_amount":
await get_amount(websocket)
else:
return
async def main():
async with serve(handler, port = 8765):
await asyncio.get_running_loop().create_future() # run forever
if __name__ == "__main__":
asyncio.run(main())