Connect to database, store location => completed endpoint

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-12 13:59:10 +02:00
parent ba4746b7c4
commit 2cdc7e1adc

21
main.py
View file

@ -1,8 +1,23 @@
import cgi import cgi
import datetime import datetime
import configparser
import pymysql import pymysql
def getDatabase(path = "config.cfg"):
config = configparser.RawConfigParser()
config.read(path)
host = config.get("Database", "host")
user = config.get("Database", "user")
pwd = config.get("Database", "pass")
name = config.get("Database", "name")
conn = pymysql.connect(host, user, pwd, name)
cur = conn.cursor()
return conn, cur
def application(env, re): def application(env, re):
if env["REQUEST_METHOD"] == "POST": if env["REQUEST_METHOD"] == "POST":
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True) args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
@ -41,5 +56,9 @@ def application(env, re):
timestr = timestamp.strftime('%Y-%m-%d %H:%M:%S') timestr = timestamp.strftime('%Y-%m-%d %H:%M:%S')
sql = "INSERT INTO tracker(ts, device, lat, lon, alt) VALUES (\"%s\", \"%s\", %s, %s, %s);" % (timestr, device, str(latitude), str(longitude), str(altitude) or "None") conn, cur = getDatabase()
sql = "INSERT INTO tracker(ts, device, lat, lon, alt) VALUES (%s, %s, %s, %s, %s);"
cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None))
conn.commit()