4gcheck.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from crypt import methods
  2. import os
  3. import sys
  4. import typing as t
  5. import json
  6. from datetime import datetime
  7. from flask import Flask, jsonify, request
  8. a = "{"
  9. b = "}"
  10. LISTENING_PORT = int(sys.argv[1])
  11. app = Flask(__name__)
  12. app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
  13. app.config['JSON_SORT_KEYS'] = False
  14. def get_user(username: str) -> t.Optional[str]:
  15. command = 'check %s 1' % username
  16. result = os.popen(command).readlines()
  17. final = result[0].strip()
  18. return final
  19. def cont_online(username: str) -> t.Optional[str]:
  20. command = 'check %s 2' % username
  21. result = os.popen(command).readlines()
  22. final = result[0].strip()
  23. return final
  24. def limiter_user(username: str) -> t.Optional[str]:
  25. command = 'check %s 3' % username
  26. result = os.popen(command).readlines()
  27. final = result[0].strip()
  28. return final
  29. def check_data(username: str) -> t.Optional[str]:
  30. command = 'check %s 4' % username
  31. result = os.popen(command).readlines()
  32. final = result[0].strip()
  33. return final
  34. def check_dias(username: str) -> t.Optional[str]:
  35. command = 'check %s 5' % username
  36. result = os.popen(command).readlines()
  37. final = result[0].strip()
  38. return final
  39. @app.route('/checkUser',methods = ['POST', 'GET'])
  40. def check_user():
  41. if request.method == 'POST':
  42. try:
  43. req_data = request.get_json()
  44. user_get = req_data.get("user")
  45. username = get_user(user_get)
  46. user = get_user(username)
  47. if user == "Not exist":
  48. return ("{0}\"username\":\"{1}\",\"count_connection\":\"Null\",\"expiration_date\":\"Null\",\"expiration_days\":\"Null\",\"limiter_user\":\"Null\"{2}" .format(a, user, b))
  49. else:
  50. return ("{0}\"username\":\"{1}\",\"count_connection\":\"{2}\",\"expiration_date\":\"{3}\",\"expiration_days\":\"{4}\",\"limiter_user\":\"{5}\"{6}" .format(a, username, cont_online(username), check_data(username), check_dias(username), limiter_user(username), b))
  51. except Exception as e:
  52. return jsonify({'error': str(e)})
  53. else:
  54. try:
  55. return 'Cannot GET /checkUser'
  56. except Exception as e:
  57. return jsonify({'error': str(e)})
  58. if __name__ == '__main__':
  59. app.run(
  60. host='0.0.0.0',
  61. port=int(sys.argv[1]) if len(sys.argv) > 1 else LISTENING_PORT,
  62. )