-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_class.py
More file actions
54 lines (44 loc) · 1.51 KB
/
server_class.py
File metadata and controls
54 lines (44 loc) · 1.51 KB
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
import mysql.connector
import sys
import os
import datetime
from flask_restful import Resource, Api, reqparse
from flask import jsonify, request
class queries(Resource):
def __init__(self):
#os.system('sudo service mysql restart')
self.mydb = mysql.connector.connect(
host = "localhost",
user = "server",
password = "5115abcd",
db = "test"
)
self.cursor = self.mydb.cursor()
self.date = datetime.datetime.now()
self.vitals = {}
self.parser = reqparse.RequestParser()
self.parser.add_argument('ID', type = int)
self.parser.add_argument('vitals')
def RESTART():
os.system('sudo service mysql restart')
def post(self):
data = self.parser.parse_args()
ID = data['ID']
vitals = data['vitals']
query = "INSERT INTO vitals (id, date, vitals) VALUES (%s, %s, %s)"
vals = (ID, self.date, vitals)
self.cursor.execute(query, vals)
self.mydb.commit()
resp = jsonify(data)
return resp
def get(self):
query = "SELECT * FROM vitals;"
self.cursor.execute(query)
rows = self.cursor.fetchall()
resp = jsonify(rows)
return resp
def INSERT(self, vitals, ID):
query = "INSERT INTO vitals (is, date, vitals) VALUES (%s, %s, %s)"
vals = (ID, self.date, vitals)
self.cursor.execute(query, vals)
self.mydb.commit()