tool.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import urllib.parse
  2. import datetime
  3. import hashlib
  4. import flask
  5. import re
  6. def get_time():
  7. return str(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"))
  8. def db_data_get(data):
  9. global set_data
  10. set_data = data
  11. def db_change(data):
  12. if set_data == 'mysql':
  13. data = data.replace('random()', 'rand()')
  14. data = data.replace('%', '%%')
  15. data = data.replace('?', '%s')
  16. return data
  17. def ip_check(d_type = 0):
  18. ip = ''
  19. if d_type == 0 and (flask.session and ('state' and 'id') in flask.session):
  20. ip = flask.session['id']
  21. if ip == '':
  22. try:
  23. ip = flask.request.environ.get('HTTP_X_REAL_IP', flask.request.environ.get('HTTP_X_FORWARDED_FOR', flask.request.remote_addr))
  24. ip = ip[0] if type(ip) == type([]) else ip
  25. if ip == '::1' or ip == '127.0.0.1':
  26. ip = flask.request.environ.get('HTTP_X_FORWARDED_FOR', flask.request.remote_addr)
  27. ip = ip[0] if type(ip) == type([]) else ip
  28. except:
  29. ip = 'error:ip'
  30. return str(ip)
  31. def savemark(data):
  32. data = re.sub("\[date\(now\)\]", get_time(), data)
  33. ip = ip_check()
  34. if not re.search("\.", ip):
  35. name = '[[user:' + ip + '|' + ip + ']]'
  36. else:
  37. name = ip
  38. data = re.sub("\[name\]", name, data)
  39. return data
  40. def url_pas(data):
  41. return urllib.parse.quote(data).replace('/','%2F')
  42. def sha224_replace(data):
  43. return hashlib.sha224(bytes(data, 'utf-8')).hexdigest()
  44. def md5_replace(data):
  45. return hashlib.md5(data.encode()).hexdigest()