added base files
This commit is contained in:
47
src/app.py
Normal file
47
src/app.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
import configparser
|
||||
import mysql.connector
|
||||
import datetime
|
||||
from wakeonlan import send_magic_packet
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
|
||||
def create_connection(config):
|
||||
mydb = mysql.connector.connect(
|
||||
host=config['database']['db_host'],
|
||||
user=config['database']['db_user'],
|
||||
password=config['database']['db_pass'],
|
||||
database=config['database']['db_table']
|
||||
)
|
||||
return mydb
|
||||
|
||||
|
||||
def verifyapikey(apikey, config):
|
||||
if apikey == config['setup']['apikey']:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@app.route('/wakeup', methods=['POST'])
|
||||
def wakeup():
|
||||
if verifyapikey(request.values['apikey'], config):
|
||||
db = create_connection(config)
|
||||
cursor = db.cursor()
|
||||
sql = """SELECT device_mac FROM devices WHERE device_name = %s;"""
|
||||
cursor.execute(sql, [request.values['device_name']])
|
||||
macaddr = cursor.fetchone()
|
||||
cursor.close()
|
||||
db.close()
|
||||
send_magic_packet(macaddr[0])
|
||||
return jsonify(result='success', timestamp=datetime.datetime.now())
|
||||
return jsonify(result='wrong api key')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
app.run(host='0.0.0.0', debug=True)
|
||||
Reference in New Issue
Block a user