thunix_api/thunix_api.py

72 lines
1.2 KiB
Python
Raw Normal View History

2020-01-22 23:17:09 +00:00
#!/usr/bin/python3
2020-01-22 18:29:15 +00:00
# thunix_api.py
import flask
from flask import Flask, request, jsonify
2020-01-22 19:28:44 +00:00
2020-01-22 23:17:09 +00:00
import psutil, datetime, time
2020-01-22 19:28:44 +00:00
2020-01-22 18:29:15 +00:00
app = Flask(__name__)
2020-01-22 19:28:44 +00:00
2020-01-22 18:29:15 +00:00
# No endpoint selected
@app.route("/")
def home():
return "The Thunix API. Please see https://wiki.thunix.net/wiki/api for more information."
app.run()
2020-01-22 19:28:44 +00:00
2020-01-22 18:29:15 +00:00
# ip_info
@app.route("/ip_info")
def ip_info():
2020-01-23 03:01:03 +00:00
#for nic, addrs in psutil.net_if_addrs().items():
2020-01-22 18:29:15 +00:00
return "IP Info"
app.run()
2020-01-22 19:28:44 +00:00
2020-01-22 18:29:15 +00:00
# uptime
@app.route("/uptime")
def uptime():
2020-01-22 23:17:09 +00:00
with open('/proc/uptime', 'r') as f:
secs = float(f.readline().split()[0])
day = secs // (24 * 3600)
secs = secs % (24 * 3600)
hour = secs // 3600
secs %= 3600
minutes = secs // 60
secs %= 60
seconds = secs
payload = [
{
"days": day,
"hours": hour,
"minutes": minutes,
"seconds": seconds
}
]
return jsonify(payload)
2020-01-22 18:29:15 +00:00
app.run()
2020-01-22 19:28:44 +00:00
2020-01-22 18:29:15 +00:00
# teapot
@app.route("/teapot")
def teapot():
2020-01-22 19:28:44 +00:00
teapots = [
{
"tea": "available",
"height": "short",
"width": "stout"
}
]
return jsonify(teapots)
app.run()
2020-01-22 18:29:15 +00:00
# main loop
2020-01-22 19:28:44 +00:00
if __name__ == "__main__": # on running python app.py
app.run() # run the flask app