-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflaskAPI.py
More file actions
45 lines (30 loc) · 1.2 KB
/
flaskAPI.py
File metadata and controls
45 lines (30 loc) · 1.2 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
import flask,json,psycopg2
from flask import request
jsonConfigFile = json.load(open("./setup/config.json"))
# ==============LOGIN DB===================
db_host = jsonConfigFile["db_host"]
db_port = jsonConfigFile["db_port"]
db_user = jsonConfigFile["db_user"]
db_password = jsonConfigFile["db_password"]
db_name = jsonConfigFile["db_name"]
# ==============LOGIN DB===================
conn = psycopg2.connect(host=db_host, port=db_port, user=db_user,
password=db_password, database=db_name)
cursor = conn.cursor()
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def home():
return '''<h1 align="center">basic api by git-malik</h1>'''
@app.route('/api/insert', methods=['GET'])
def insert():
customerId = request.args.get("customerId")
cpeId = request.args.get("cpeId")
if not customerId:
return '''ERROR: you must specity a customer id''', 400
if not cpeId:
return '''ERROR: you must specify a cpe id''', 400
cursor.execute("INSERT INTO customers_cpe (customer_id, customer_cpe) VALUES(%s, %s)", (customerId, cpeId))
conn.commit()
return '''DONE'''
app.run(host="0.0.0.0") # replace with an ip if needed