munyal-client/http_server.py

69 lines
1.4 KiB
Python
Raw Normal View History

2019-07-28 16:18:49 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2019-08-18 01:09:33 +00:00
import json
from hashlib import md5
2019-07-28 16:18:49 +00:00
from random import randint
from time import time
2019-08-18 01:09:33 +00:00
from flask import Flask, jsonify, request
import rethinkdb as r
2019-07-28 16:18:49 +00:00
app = Flask(__name__)
2019-08-18 01:09:33 +00:00
2019-07-28 16:18:49 +00:00
def md5sum(filename):
try:
hash = md5()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(128 * hash.block_size), b""):
hash.update(chunk)
return hash.hexdigest()
except:
return None
2019-08-18 01:09:33 +00:00
2019-07-28 16:18:49 +00:00
@app.route("/", methods=["GET"])
def index():
2019-08-18 01:09:33 +00:00
return ('''
2019-07-28 16:18:49 +00:00
<html>
<head>
<title>Munyal API</title>
</head>
<body>
<h1>Munyal private API</h1>
</body>
</html>
''')
2019-08-18 01:09:33 +00:00
2019-07-28 16:18:49 +00:00
@app.route("/upload", methods=["POST"])
def upload():
try:
2019-08-18 01:09:33 +00:00
r.connect("localhost", 28015).repl()
2019-07-28 16:18:49 +00:00
cursor = r.table("changes")
2019-08-18 01:09:33 +00:00
2019-07-28 16:18:49 +00:00
host = request.form.get("host")
action = request.form.get("action")
route = request.form.get("route")
obj = {
2019-08-18 01:09:33 +00:00
'id': str(time()).split('.')[0] + str(randint(1, 1000000)),
2019-07-28 16:18:49 +00:00
'action': action,
'route': route,
'host': host
}
status = 'ok'
try:
cursor.insert(obj).run()
except:
status = 'error'
except:
status = 'error'
obj['status'] = status
return jsonify(obj)
2019-08-18 01:09:33 +00:00
2019-07-28 16:18:49 +00:00
if __name__ == '__main__':
app.run(debug=True)