added base files
parent
b36f718908
commit
529ebba77b
|
@ -0,0 +1,4 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
.idea/
|
||||
/venv/
|
||||
/src/config.ini
|
|
@ -0,0 +1,8 @@
|
|||
FROM python:3-alpine
|
||||
|
||||
COPY . /code/
|
||||
WORKDIR /code/
|
||||
|
||||
RUN pip install -r /code/requirements.txt
|
||||
|
||||
CMD ["python","src/api.py"]
|
|
@ -0,0 +1,11 @@
|
|||
click==7.1.2
|
||||
Flask==1.1.2
|
||||
Flask-Cors==3.0.10
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.11.2
|
||||
MarkupSafe==1.1.1
|
||||
mysql-connector-python==8.0.23
|
||||
protobuf==3.14.0
|
||||
six==1.15.0
|
||||
wakeonlan==1.1.6
|
||||
Werkzeug==1.0.1
|
|
@ -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)
|
Loading…
Reference in New Issue