def execute_refresh_clicklog(times=30): """从redis读取访问记录刷入mysql""" key = "passport:AccessCount:clicklog" while times > 0: data = redis.lpop(key) try: data = json.loads(data) except: cli_logger.info("no clicklog") break else: browserType, browserDevice, browserOs, browserFamily = parse_userAgent( data["agent"]) sql = "INSERT INTO sys_clicklog set url=%s, ip=%s, agent=%s, method=%s, status_code=%s, referer=%s, isp=%s, browserType=%s, browserDevice=%s, browserOs=%s, browserFamily=%s, clickTime=%s, TimeInterval=%s" try: mysql.insert(sql, data.get("url"), data.get("ip"), data.get("agent"), data.get("method"), data.get("status_code"), data.get("referer"), getIpArea(data.get("ip")), browserType, browserDevice, browserOs, browserFamily, int(data.get("clickTime") or 0), data.get("TimeInterval")) except Exception, e: cli_logger.warn(e, exc_info=True) else: cli_logger.info("refresh_clicklog is ok")
def usersecurity(): res = dict(msg=None, code=1) Action = request.args.get("Action") if request.method == "GET": if Action == "getSessions": sd = analysis_sessionId(request.cookies.get("sessionId")) if sd: res.update(code=0) data = dict() # 获取当前会话 if request.args.get("getCurrentSession", True) in (True, "True", "true"): browserType, browserDevice, browserOs, browserFamily = parse_userAgent(request.headers.get("User-Agent")) area = getIpArea(g.ip) if len(area.split()) >= 3: area = area.split()[2] CurrentSession = dict(iat=sd['iat'], exp=sd['exp'], browser=dict(family=" ".join(browserFamily.split()[:-1]), os=browserOs), ip=g.ip, area=area) if g.sid: CurrentSession["session"] = g.api.usersso.ssoGetWithSid(g.sid, True) data["CurrentSession"]=CurrentSession # 获取其他会话 if request.args.get("getOtherSession") in (True, "True", "true"): OtherSession = [ g.api.usersso.ssoGetWithSid(sid, True) for sid in g.api.usersso.ssoGetRegisteredUserSid(g.uid) if g.sid != sid and g.api.usersso.ssoExistWithSid(sid) ] data["OtherSession"] = OtherSession res["data"] = data elif Action == "getLoginHistory": # 获取登录历史 sort = request.args.get("sort") or "desc" page = request.args.get("page") or 1 limit = request.args.get("limit") or 10 res = g.api.userprofile.listUserLoginHistory(uid=g.uid, page=page, limit=limit, sort=sort) logger.info(res) return jsonify(dfr(res))
def execute_refresh_loginlog(times=30): """从redis读取登录记录刷入mysql""" key = "passport:loginlog" while times > 0: data = redis.lpop(key) try: data = json.loads(data) except: cli_logger.info("no loginlog") else: browserType, browserDevice, browserOs, browserFamily = parse_userAgent(data["user_agent"]) login_area = getIpArea(data["login_ip"]) sql = "INSERT INTO user_loginlog (uid, login_type, login_ip, login_area, login_time, user_agent, browser_type, browser_device, browser_os, browser_family) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" try: mysql.insert(sql, data["uid"], data["identity_type"], data["login_ip"], login_area, data["login_time"], data["user_agent"], browserType, browserDevice, browserOs, browserFamily) except Exception, e: cli_logger.warn(e, exc_info=True) else: cli_logger.info("refresh_loginlog is ok")