def deletePass(user, session): guest_session_id = request.get_cookie("guest") Session.deleteGuest(guest_session_id) response.delete_cookie("guest") response.delete_cookie("token") response.delete_cookie("session") return resJSON(1, "bye")
def wrapper(*args, **kw): users = readUsers() cookie_session = request.get_cookie('session') guest_session_id = request.get_cookie("guest") guest_session = Session.getGuest(guest_session_id) private = Session.getGuest_key(guest_session_id, 'privateKey') user = None if guest_session is None or 'user' not in guest_session else guest_session['user'] user_ans = session_ans = de_session = session_id = None try: if user and cookie_session and private: de_session = json.loads(deAES(users[user], cookie_session)) # 第一层验证 cookie解密 if user in users and de_session['user'] == user: # 第二层验证 session session_id = de_session['session_id'] session = Session.get(user, session_id) # session 时间戳不活跃过期机制 if float(de_session['time']) + session_timeout > time.time() and \ session and session['user'] == user: user_ans = user session_ans = session except Exception: response.delete_cookie('guest') response.delete_cookie('session') temp = func(user=user_ans, session=session_ans, *args, **kw) if user_ans: de_session['time'] = time.time() de_session['session_id'] = Session.update(user, session_id, session_ans) encrypted = enAES(users[user], json.dumps(de_session)) response.set_cookie("session", encrypted, path='/') return temp
def registerAjax(): users = readUsers() user = request.forms.get("user") password = request.forms.get("password") challenge = request.forms.get("validate[geetest_challenge]") validate = request.forms.get("validate[geetest_validate]") seccode = request.forms.get("validate[geetest_seccode]") result = gt.geetest_validate(challenge, validate, seccode) if not result: return resJSON(0, "Validate Fail ") guest_session_id = request.get_cookie("guest") private = Session.getGuest_key(guest_session_id, "privateKey") try: de_user = deRSA(user, private) de_password = deRSA(password, private) flag, msg = addUser(de_user, de_password, False) if flag: return resJSON(1, msg) else: return resJSON(0, msg) except Exception, e: print e
def login(): users = readUsers() user = request.forms.get("user") password = request.forms.get("password") guest_session_id = request.get_cookie("guest") private = Session.getGuest_key(guest_session_id, "privateKey") try: de_user = deRSA(user, private) de_password = deRSA(password, private) print de_user, de_password print users[de_user] if de_user in users and users[de_user] == de_password: session_id = Session.set(de_user, None, {"user": de_user}) encrypted = enAES(de_password, json.dumps({"user": de_user, "time": time.time(), "session_id": session_id})) new_guest = Session.updateGuest(guest_session_id, {"user": de_user}) response.set_cookie("guest", new_guest, path="/") response.set_cookie("session", encrypted, path="/") return resJSON(1, "ok") except Exception, e: print e
def get_liquidity(ex_pair: str = Path( ..., regex="^(xwc_eth|xwc_tp|xwc_cusd|all)$")): session = Session() if ex_pair == 'all': data = session.query(StSwapLiquidity).order_by( StSwapLiquidity.stat_time).all() else: data = session.query(StSwapLiquidity).\ filter(StSwapLiquidity.tp_name==ex_pair).\ order_by(StSwapLiquidity.stat_time).all() liquidity = [] xt_api = Api("", "") price = { 'XWC': xt_api.get_ticker(f'xwc_usdt')['price'], 'ETH': xt_api.get_ticker(f'eth_usdt')['price'], 'TP': xt_api.get_ticker(f'tp_usdt')['price'], 'CUSD': 1 } print(price) dailyData = {"stat_time": 0, "market_value": -1} for d in data: if dailyData['stat_time'] == d.stat_time: dailyData['market_value'] += d.token1_amount / 10**8 * price[ d.token1_name] + d.token2_amount / 10**8 * price[d.token2_name] else: if dailyData['stat_time'] != 0: liquidity.append(copy.deepcopy(dailyData)) dailyData['stat_time'] = d.stat_time dailyData['market_value'] = d.token1_amount / 10**8 * price[ d.token1_name] + d.token2_amount / 10**8 * price[d.token2_name] liquidity.append(copy.deepcopy(dailyData)) session.close() return liquidity
def register(): response.set_cookie("token", str(random.random()), path="/") (public, private) = createRSA() guest = request.get_cookie("guest") new_guest = Session.updateGuest(guest, {"privateKey": private}) response.set_cookie("guest", new_guest, path="/") return dict( title="Register", publickey=public, versions=1, GeetestParam={"id": captcha_id, "challenge": gt.geetest_register()}, )
def index(user, session): response.set_cookie("token", str(random.random()), path="/") (public, private) = createRSA() guest = request.get_cookie("guest") new_guest = Session.updateGuest(guest, {"privateKey": private}) if user is not None: title = "Hello %s !" % user else: title = "Hello !" # response.delete_cookie("guest") response.set_cookie("guest", new_guest, path="/") return dict(title=title, user=user, publickey=public, versions=1)
def get_kline(ex_pair: str = Path(..., regex="^(xwc_eth|xwc_tp|xwc_cusd)$"), k_type: int = Path(0, ge=0, lt=11), limit: int = 100): session = Session() if k_type < 0 or k_type >= len(kline_table_list): print(f"invalid k_type [MUST between 0 - {len(kline_table_list)-1}]") return f"invalid k_type [MUST between 0 - {len(kline_table_list)-1}]" cycles = [ 60, 300, 900, 1800, 3600, 7200, 21600, 43200, 86400, 604800, 2592000 ] k_table = kline_table_list[k_type] #print(k_table, ex_pair) now = datetime.datetime.utcnow() start = now - datetime.timedelta(seconds=(limit * cycles[k_type] + 1)) print(start.strftime("%Y-%m-%d %H:%M:%S")) missing_position = 0 try: data = session.query(k_table).filter(k_table.ex_pair==ex_pair, k_table.timestamp>=start).\ order_by(k_table.timestamp).all() except Exception as e: print(str(e)) return [] if len(data) == 0: data = session.query(k_table).filter(k_table.ex_pair==ex_pair).\ order_by(k_table.timestamp.desc()).limit(1).all() if len(data) == 1: data[0].volume = 0 data[0].block_num = 0 else: missing_position = len(data) if data is None or len(data) == 0: return [] last_item = copy.deepcopy(data[len(data) - 1]) last_item.volume = 0 last_item.block_num = 0 last_item.k_open = last_item.k_close last_item.k_high = last_item.k_close last_item.k_low = last_item.k_close while True: last_item.timestamp += datetime.timedelta(seconds=cycles[k_type]) if last_item.timestamp > now: break if missing_position == 0: data = [] for i in range(missing_position, limit): # logging.info(last_item.timestamp) last_item.timestamp -= datetime.timedelta(seconds=cycles[k_type]) if missing_position > 0 and last_item.timestamp <= data[ missing_position - 1].timestamp: break else: data.insert(missing_position, copy.deepcopy(last_item)) session.close() return data
def editPass(user, session): users = readUsers() guest_key = request.get_cookie("guest") private = Session.getGuest_key(guest_key, "privateKey") try: if user and user in users and private: item = request.forms.item item = deRSA(item, private) item = item.split("|") id = item[1] value = item[0] allPass = loadPassword(user) allPass[id] = value savePassword(user, allPass) return resJSON(1, "save ok") except Exception: print Exception return resJSON(0, "Error. try refresh the page")
def get_trade_amount(cycle: str = Path(..., regex="^(daily|weekly)$")): session = Session() if cycle == 'daily': data = session.query( StSwapKdataDaily.timestamp, func.sum(StSwapKdataDaily.volume).label("volume")).group_by( StSwapKdataDaily.timestamp).all() elif cycle == 'weekly': data = session.query( StSwapKdataWeekly.timestamp, func.sum(StSwapKdataWeekly.volume).label("volume")).group_by( StSwapKdataWeekly.timestamp).all() xt_api = Api("", "") xwcPrice = xt_api.get_ticker(f'xwc_usdt')['price'] result = [] for d in data: result.append({ 'stat_time': d.timestamp, 'trade_amount': float(d.volume) * xwcPrice }) session.close() return result