cron-api.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/python3
  2. import os
  3. import subprocess
  4. from flask import Flask, Response, json, request
  5. from cron.Parser import Parser
  6. api = Flask(__name__)
  7. @api.route('/version', methods=['GET'])
  8. def get_version():
  9. return Response(json.dumps({ "version": "1.0" }), mimetype="application/json")
  10. @api.route('/section/<string:name>', methods=['GET'])
  11. def get_section(name):
  12. cronFile = Parser().parse()
  13. return Response(cronFile.get_section(name), mimetype="text/plain")
  14. @api.route('/section/<string:name>', methods=['POST'])
  15. def set_section(name):
  16. parser = Parser()
  17. cronFile = parser.parse()
  18. cronFile.set_section(name, request.data.decode())
  19. parser.update(cronFile)
  20. cronFile = Parser().parse()
  21. return Response(cronFile.get_section(name), mimetype="text/plain")
  22. if __name__ == "__main__":
  23. print("Running as user:")
  24. subprocess.run("whoami")
  25. print("Checking crontab access with 'crontab -l'")
  26. print(subprocess.run(["crontab", "-l"], capture_output = True, text = True).stdout.count('\n'))
  27. api.run(host = "0.0.0.0", port = 5000)