-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
53 lines (39 loc) · 1.49 KB
/
Copy pathserver.py
File metadata and controls
53 lines (39 loc) · 1.49 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
from flask import Flask
from flask import request
import sqlite3
from datetime import datetime
import traceback
conn = None
def check_connection(conn):
try:
conn.cursor()
except:
conn = sqlite3.connect('./instances.db')
return conn
def increase_counter(conn, id_):
conn = check_connection(conn)
c = conn.cursor()
if not c.execute(f"SELECT COUNT(*) FROM objects where id = {id_}").fetchone()[0]:
c.execute(f"INSERT INTO locations (id, location) values ({id_}, {-1})")
location = c.execute(f"SELECT location from locations where id == {id_} limit 1").fetchone()[0]
c.execute(f"INSERT INTO objects (id, location, date) values ({id_}, {location}, '{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}')")
conn.commit()
def change_location(conn, id_, location):
conn = check_connection(conn)
c = conn.cursor()
if not c.execute(f"SELECT COUNT(*) FROM objects where id = {id_}"):
c.execute(f"INSERT INTO locations (id, location) values ({id_}, {location}")
c.execute(f"UPDATE SET id = {id_}, location = {location} where id = {id_}")
conn.commit()
app = Flask(__name__)
@app.route('/counter')
def counter():
try:
increase_counter(conn, request.args.get('id'))
except Exception as e:
return str(traceback.print_exc())
return "OK"
@app.route('/changelocation')
def change_loc():
change_location(conn, request.args.get('id'), request.args.get('location'))
return f"LOC: {request.args.get('location')}"