def connection_made(self, address, stream): s = address[0] + ":" + str(address[1]) if s in self.blacklist: stream.close() return self.global_connection[s] = stream echo(f'{address} connected!', falg='INFO')
def encode_auth_token(user_info): """ 生成认证Token :param user_id: int :param login_time: int(timestamp) :return: string """ """ "exp": 过期时间 "nbf": 表示当前时间在nbf里的时间之前,则Token不被接受 "iss": token签发者 "aud": 接收者 "iat": 发行时间 """ try: payload = { 'exp': datetime.datetime.now() + datetime.timedelta(days=2, hours=0, seconds=0), # 'exp': datetime.datetime.now() + datetime.timedelta(seconds=10), # test 'iat': datetime.datetime.now(), 'iss': 'looper_me', 'data': user_info } return jwt.encode( payload, JWT_SECRET_KEY, algorithm='HS256' ) except Exception as e: echo(e, flag='ERROR') return e
async def wrapper(self): result = Auth.identify(self.request) if result['success'] and result['data']: echo("Token验证成功", flag='SUCCESS') self.current_user = result['data'] await view_func(self) else: echo("Token验证失败: " + result['msg'], flag='ERROR') self.set_status(302) self.finish()
def wrapper(self): result = Auth.identify(self.request) if result['success'] and result['data']: echo("Token验证成功", flag='SUCCESS') self.current_user = result['data'] view_func(self) else: echo("Token验证失败: " + result['msg'], flag='ERROR') result.update({"token": False}) self.write(result)
def post(self): ip = self.get_argument('ip', None) todo = self.get_argument('todo', None) if not ip or not todo: return echo(todo, ip) if todo == 'alive': if ip in md_server.blacklist: md_server.blacklist.remove(ip) blacklist_db.delete(ip) self.write(true_return(msg='解封成功')) else: self.write(false_return(msg='解封失败'))
async def post(self): code = self.get_argument('code') start = self.get_argument('start', None) end = self.get_argument('end', None) filename = '' data_csv = '' """ 检查参数,转换参数,设定文件名""" if not code: return code = code.split('+') try: if start: filename += start + "_" start = datetime.strptime(start, '%Y-%m-%d') if end: filename += end + "_" end = datetime.strptime(end, '%Y-%m-%d') except ValueError: self.write(false_return(msg='日期参数格式错误')) return filename = '{}{}.csv'.format(filename, code[0] if len(code) == 1 else 'Many') echo(type(code), code) """ 过滤查询 """ results = await filter(code, start, end, download=True) """ 处理 """ for item in results: if isinstance(item, dict): try: item['datetime'] = datetime.strftime( item['datetime'], '%Y-%m-%d %H:%M:%S') except KeyError: item['datetime'] = datetime.strftime( item['datetime'], '%Y-%m-%d %H:%M:%S.%f') item = str(item).replace('{', '').replace('}', '') data_csv += '{},\r\n'.format(item, ) """写入""" self.set_header('Content-Type', 'application/octet-stream') self.set_header( 'Content-Disposition', 'filename={}'.format( filename.encode('utf-8').decode('ISO-8859-1'))) self.write(data_csv) self.finish()
def post(self): ip = self.get_argument('ip', None) todo = self.get_argument('todo', None) if not ip or not todo: return echo(todo, ip) if ip in md_server.global_connection: md_server.global_connection.pop(ip).close() # 断开连接 if ip in md_server.tick_origin: md_server.tick_origin.remove(ip) # 源服务器弹出 if todo == 'kill': self.write(true_return(msg='封禁成功')) if todo == 'pull_black': md_server.blacklist.add(ip) # 拉黑 blacklist_db.add(ip) # 存入数据库 self.write(true_return(msg='拉黑成功')) else: self.write(false_return(msg='操作失败'))
from application import make_app from application.common import echo from application.tcp_server import md_server, trade_server from application.model import config_db, admin_db config_db.system_start() admin_db.system_start() if __name__ == '__main__': try: cfg = ConfigParser() cfg.read('serve_config.ini') md_port = cfg.getint('serve', 'md_port') td_port = cfg.getint('serve', 'td_port') http_port = cfg.getint('serve', 'http_port') md_server.listen(md_port) md_server.start() trade_server.listen(td_port) trade_server.start() app = make_app() app.listen(http_port) echo( f"""\n '数据服务器成功启动^_^ : \n 行情服务器地址---------> tcp://127.0.0.1:{md_port}\n 交易服务器地址---------> tcp://127.0.0.1:{td_port} # 暂未启用\n http服务器-------------> http://127.0.0.1:{http_port} '""", category="INFO") except Exception as e: echo('启动失败, 请检查端口是否被占用', category='ERROR') IOLoop.instance().start()