easyctf-2017/server/api/utils.py

44 lines
1.2 KiB
Python
Raw Normal View History

2015-12-23 03:44:51 +00:00
import datetime
2015-12-23 23:23:18 +00:00
import json
2015-12-23 03:44:51 +00:00
import random
import string
2015-12-23 23:23:18 +00:00
import traceback
2015-12-23 03:44:51 +00:00
2015-12-23 23:23:18 +00:00
from functools import wraps
2015-12-23 03:44:51 +00:00
from werkzeug.security import generate_password_hash, check_password_hash
2015-12-23 23:23:18 +00:00
class WebException(Exception): pass
2015-12-23 03:44:51 +00:00
def hash_password(s):
2015-12-23 23:23:18 +00:00
return generate_password_hash(s)
2015-12-23 03:44:51 +00:00
def check_password(hashed_password, try_password):
2015-12-23 23:23:18 +00:00
return check_password_hash(hashed_password, try_password)
2015-12-23 03:44:51 +00:00
def generate_string(length):
2015-12-23 23:23:18 +00:00
return "".join([random.choice(string.letters + string.digits) for x in range(length)])
2015-12-23 03:44:51 +00:00
def unix_time_millis(dt):
2015-12-23 23:23:18 +00:00
epoch = datetime.datetime.utcfromtimestamp(0)
return (dt - epoch).total_seconds() * 1000.0
2015-12-23 03:44:51 +00:00
def get_time_since_epoch():
2015-12-23 23:23:18 +00:00
return unix_time_millis(datetime.datetime.now())
def api_wrapper(f):
@wraps(f)
def wrapper(*args, **kwds):
web_result = {}
response = 200
try:
web_result = f(*args, **kwds)
except WebException as error:
response = 200
web_result = { "success": 0, "message": str(error) }
except Exception as error:
response = 200
traceback.print_exc()
web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() }
return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" }
2015-12-24 00:40:59 +00:00
return wrapper