class Syslog: logger = None levels = { "DEBUG" : logging.DEBUG, "INFO" : logging.INFO, "WARNING" : logging.WARNING, "ERROR" : logging.ERROR, "CRITICAL" : logging.CRITICAL } log_level = GLOBAL.get('LogLevel', "DEBUG") log_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'logs') if not os.path.exists(log_dir): os.mkdir(log_dir) log_file = os.path.join(log_dir, 'sys.log') log_backup_count = 50 log_datefmt = '%Y-%m-%d %H:%M:%S' @staticmethod def getLogger(): if Syslog.logger is not None: return Syslog.logger Syslog.logger = logging.Logger("loggingmodule.Syslog") log_handler = logging.handlers.TimedRotatingFileHandler(filename = Syslog.log_file, backupCount = Syslog.log_backup_count, when = "midnight", ) log_fmt = logging.Formatter('[ %(levelname)s ] %(asctime)s %(filename)s:%(threadName)s:%(lineno)d %(message)s', datefmt=Syslog.log_datefmt) log_handler.setFormatter(log_fmt) Syslog.logger.addHandler(log_handler) Syslog.logger.setLevel(Syslog.levels.get(Syslog.log_level)) return Syslog.logger
def __init__(self, logName, backupCount=10): self.logName = logName self.log_dir = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'logs') self.logFile = os.path.join(self.log_dir, '{0}.log'.format(self.logName)) self._levels = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL } self._logfmt = '%Y-%m-%d %H:%M:%S' self._logger = logging.getLogger(self.logName) if not os.path.exists(self.log_dir): os.mkdir(self.log_dir) handler = logging.handlers.TimedRotatingFileHandler( filename=self.logFile, backupCount=backupCount, when="midnight") handler.suffix = "%Y%m%d" formatter = logging.Formatter( '[ %(levelname)s ] %(asctime)s %(filename)s:%(threadName)s:%(lineno)d %(message)s', datefmt=self._logfmt) handler.setFormatter(formatter) self._logger.addHandler(handler) self._logger.setLevel(self._levels.get(GLOBAL.get('LogLevel', "INFO")))
def __init__(self, logname, backup_count=10): self.logname = logname self.log_dir = join(dirname(dirname(abspath(__file__))), "logs") self.log_file = join(self.log_dir, "{0}.log".format(self.logname)) self._levels = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL, } self._logfmt = "%Y-%m-%d %H:%M:%S" self._logger = logging.getLogger(self.logname) if not exists(self.log_dir): mkdir(self.log_dir) LEVEL = GLOBAL.get("LogLevel", "INFO").upper() if LEVEL == "DEBUG": LOGFMT = ("[ %(levelname)s ] %(threadName)s %(asctime)s " "%(filename)s:%(lineno)d %(message)s") else: LOGFMT = ("[ %(levelname)s ] %(asctime)s " "%(filename)s:%(lineno)d %(message)s") handler = logging.handlers.TimedRotatingFileHandler( filename=self.log_file, backupCount=backup_count, when="midnight") handler.suffix = "%Y%m%d" formatter = logging.Formatter(LOGFMT, datefmt=self._logfmt) handler.setFormatter(formatter) self._logger.addHandler(handler) self._logger.setLevel(self._levels.get(LEVEL))
def put2Redis(): def Ips(): _WanIps, _LanIps = get_ip(getLanIp=True) logger.debug("wanip(%s), lanip(%s)" % (_WanIps, _LanIps)) WanIp = _WanIps if len( _WanIps.split(",")) == 1 else _WanIps.split(",")[0] LanIp = _LanIps if len( _LanIps.split(",")) == 1 else _LanIps.split(",")[0] return WanIp, LanIp, _WanIps, _LanIps WanIp, LanIp, _WanIps, _LanIps = Ips() Host = GLOBAL.get('Host') Port = GLOBAL.get('Port') LogLevel = GLOBAL.get('LogLevel') ProcessName = PRODUCT.get('ProcessName') ProductType = PRODUCT.get('ProductType') while True: if ip_check(WanIp): logger.info("You will register something information into redis.") data = { "application_name": ProcessName, "application_port": Port, "application_loglevel": LogLevel, "application_wanip": WanIp, "application_lanip": _LanIps, "application_protype": ProductType } req = requests.post("https://passport.saintic.com/registry/", data=data, verify=False, timeout=5) try: res = req.json() if res == True: logger.info("Register redis success!") else: logger.error("Register redis fail!!!") except Exception, e: logger.error(e) else: logger.warn("ip invaild, continue.") WanIp, LanIp, _WanIps, _LanIps = Ips() continue time.sleep(10)
# -*- coding:utf8 -*- import os import logging.handlers from config import GLOBAL loglevel = GLOBAL.get('LogLevel', "INFO") CODE_HOME = os.path.dirname(os.path.abspath(__file__)) class Syslog: logger = None levels = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL } log_level = loglevel log_dir = os.path.join(os.path.dirname(CODE_HOME), 'logs') if not os.path.exists(log_dir): os.mkdir(log_dir) log_file = os.path.join(log_dir, 'sys.log') log_backup_count = 10 log_datefmt = '%Y-%m-%d %H:%M:%S' @staticmethod def getLogger(): if Syslog.logger is not None:
for i in sorted(data, key=lambda k: (k.get('ctime', 0), k.get('imgUrl', 0)), reverse=True) ][:15] feed = AtomFeed(g.site["site_RssTitle"], subtitle='Cherry Blossoms', feed_url=request.url, url=request.url_root, icon=url_for('static', filename='images/favicon.ico', _external=True), author=__author__) for img in data: title = timestamp_datetime(float(img['ctime'])) content = u'<img src="{}">'.format(img['imgUrl']) feed.add(title, content, content_type='html', id=img['imgId'], url=img['imgUrl'], author=__author__, updated=datetime.datetime.fromtimestamp(float(img['ctime'])), published=datetime.datetime.fromtimestamp(float( img['ctime']))) return feed.get_response() if __name__ == '__main__': Host = GLOBAL.get('Host') Port = GLOBAL.get('Port') app.run(host=Host, port=int(Port), debug=True)
@app.route('/login/') def login(): if g.signin: return redirect(url_for("front.index")) else: ReturnUrl = request.args.get("NextUrl") or request.args.get( "ReturnUrl") SSOLoginURL = url_for( "sso.Login", ReturnUrl=ReturnUrl) if ReturnUrl else url_for("sso.Login") return redirect(SSOLoginURL) @app.route('/logout/') def logout(): # 注销 if g.signin: return redirect(url_for("sso.Logout")) else: return redirect(url_for("sso.Login")) @app.route('/SignUp') def signup(): regUrl = SSO.get("sso_server").strip("/") + "/signUp" return redirect(regUrl) if __name__ == '__main__': app.run(host=GLOBAL.get('Host'), port=int(GLOBAL.get('Port')), debug=True)
#-*- coding:utf8 -*- import os import logging.handlers from config import GLOBAL loglevel = GLOBAL.get('LogLevel', "DEBUG") CODE_HOME = os.path.dirname(os.path.abspath(__file__)) class Syslog: logger = None levels = { "DEBUG": logging.DEBUG, "INFO": logging.INFO, "WARNING": logging.WARNING, "ERROR": logging.ERROR, "CRITICAL": logging.CRITICAL } log_level = loglevel log_dir = os.path.join(os.path.dirname(CODE_HOME), 'logs') if not os.path.exists(log_dir): os.mkdir(log_dir) log_file = os.path.join(log_dir, 'sys.log') log_backup_count = 50 log_datefmt = '%Y-%m-%d %H:%M:%S' @staticmethod def getLogger(): if Syslog.logger is not None: return Syslog.logger
def get(self): code = request.args.get("code") SSORequest = True if request.args.get("sso") in ("true", "True", True, "1", "on") else False SSOProject = request.args.get("sso_p") SSORedirect = request.args.get("sso_r") SSOToken = request.args.get("sso_t") SSOTokenMD5 = md5("%s:%s" % (SSOProject, SSORedirect)) logger.debug(request.args) logger.debug(SSOTokenMD5 == SSOToken) if g.signin: return redirect(url_for("uc")) elif code: SSOLoginURL = "%s?%s" % ( PLUGINS['thirdLogin']['GITHUB']['REDIRECT_URI'], urlencode({ "sso": SSORequest, "sso_r": SSORedirect, "sso_p": SSOProject, "sso_t": SSOToken })) logger.debug(SSOLoginURL) data = GitHub_Login_Page_State( code, PLUGINS['thirdLogin']['GITHUB']['APP_ID'], PLUGINS['thirdLogin']['GITHUB']['APP_KEY'], SSOLoginURL) if data: username = data.get("username") expires_in = 3600 * 24 * 30 userid = data.get("uid") expire_time = How_Much_Time( seconds=expires_in) if expires_in else None sessionId = md5( '%s-%s-%s-%s' % (username, userid, expire_time, "COOKIE_KEY")).upper() if SSOProject in GLOBAL.get( "ACL" ) and SSORequest and SSORedirect and SSOTokenMD5 == SSOToken: logger.info( "RequestURL:%s, SSORequest:%s, SSOProject:%s, SSORedirect:%s" % (request.url, SSORequest, SSOProject, SSORedirect)) ticket = '.'.join([username, expire_time, sessionId]) returnURL = SSORedirect + "?ticket=" + ticket logger.info( "SSO(%s) request project is in acl, will create a ticket, redirect to %s" % (SSOProject, returnURL)) resp = make_response(redirect(returnURL)) else: logger.info("Not SSO Auth, to local auth") resp = make_response(redirect(url_for("uc"))) resp.set_cookie(key='logged_in', value="yes", max_age=expires_in) resp.set_cookie(key='username', value=username, max_age=expires_in) resp.set_cookie(key='time', value=expire_time, max_age=expires_in) resp.set_cookie(key='Azone', value="GitHub", max_age=expires_in) resp.set_cookie( key='sessionId', value=md5( '%s-%s-%s-%s' % (username, userid, expire_time, "COOKIE_KEY")).upper(), max_age=expires_in) return resp else: return redirect(url_for("login"))
from utils.web import get_site_config, JsonResponse, default_login_auth, \ get_redirect_url, change_userinfo from utils.cli import sa_cli from libs.hook import HookManager from config import GLOBAL from version import __version__ __author__ = 'staugur' __email__ = '*****@*****.**' __date__ = '2019-12-20' __doc__ = 'Flask-based web self-built pictures bed' app = Flask(__name__) app.response_class = JsonResponse app.config.update( SECRET_KEY=GLOBAL.get("SecretKey") or str(uuid4()), MAX_CONTENT_LENGTH=10 * 1024 * 1024, ) hm = HookManager(app) rc = create_redis_engine() app.register_blueprint(front_bp) app.register_blueprint(api_bp, url_prefix="/api") app.cli.add_command(sa_cli) @app.context_processor def GlobalTemplateVariables(): return {"Version": __version__, "Doc": __doc__, "is_true": is_true}
def get(self): conf = init_pribo(GLOBAL.get('INIT_POINT')) print 'conf is' print conf return conf
from flask_restful import Resource, Api, reqparse from pribo.bayesian_optimization import * app = Flask(__name__) api = Api(app) bo = BayesianOptimization( pbounds={ 'cpu_count': (2, 8), 'ram': (4, 16), 'diskType': (0, 1), 'netType': (0, 1), 'count': (2, 5) }) init_point_size = GLOBAL.get('INIT_POINT') init_flag = False parser = reqparse.RequestParser() parser.add_argument('count') parser.add_argument('ram') parser.add_argument('cpu_count') parser.add_argument('time') parser.add_argument('netType') parser.add_argument('diskType') class PriBO(Resource): def get(self): conf = init_pribo(GLOBAL.get('INIT_POINT'))
def login(): SSORequest = True if request.args.get("sso") in ("true", "True", True, "1", "on") else False SSOProject = request.args.get("sso_p") SSORedirect = request.args.get("sso_r") SSOToken = request.args.get("sso_t") SSOTokenMD5 = md5("%s:%s" % (SSOProject, SSORedirect)) logger.debug(request.args) logger.debug("remember: %s" % request.form) logger.debug(SSOTokenMD5 == SSOToken) if g.signin: if SSOProject in GLOBAL.get( "ACL" ) and SSORequest and SSORedirect and SSOTokenMD5 == SSOToken: returnURL = SSORedirect + "?ticket=" + g.credential logger.info( "SSO(%s) request project is in acl, already landing, redirect to %s" % (SSOProject, returnURL)) return redirect(returnURL) else: return redirect(url_for("uc")) else: if request.method == "GET": return render_template( "signin.html", enable_qq=PLUGINS['thirdLogin']['QQ']['ENABLE'], enable_weibo=PLUGINS['thirdLogin']['WEIBO']['ENABLE'], enable_github=PLUGINS['thirdLogin']['GITHUB']['ENABLE'], enable_instagram=PLUGINS['thirdLogin']['INSTAGRAM']['ENABLE'], enable_oschina=PLUGINS['thirdLogin']['OSCHINA']['ENABLE']) else: username = request.form.get("username") password = request.form.get("password") remember = 30 if request.form.get("remember") in ("True", "true", True) else None if username and password and UserAuth_Login(username, password): max_age_sec = 3600 * 24 * remember if remember else None expires = How_Much_Time(max_age_sec) if max_age_sec else 'None' #expire_time = datetime.datetime.today() + datetime.timedelta(days=remember) if remember else None sessionId = md5( '%s-%s-%s-%s' % (username, md5(password), expires, "COOKIE_KEY")).upper() logger.debug( "check user login successful, max_age_sec: %s, expires: %s" % (max_age_sec, expires)) if SSOProject in GLOBAL.get( "ACL" ) and SSORequest and SSORedirect and SSOTokenMD5 == SSOToken: logger.info( "RequestURL:%s, SSORequest:%s, SSOProject:%s, SSORedirect:%s" % (request.url, SSORequest, SSOProject, SSORedirect)) ticket = '.'.join([username, expires, sessionId]) returnURL = SSORedirect + "?ticket=" + ticket logger.info( "SSO(%s) request project is in acl, will create a ticket, redirect to %s" % (SSOProject, returnURL)) resp = make_response(redirect(returnURL)) else: logger.info("Not SSO Auth, to local auth") resp = make_response(redirect(url_for("uc"))) resp.set_cookie(key='logged_in', value="yes", max_age=max_age_sec) resp.set_cookie(key='username', value=username, max_age=max_age_sec) resp.set_cookie(key='sessionId', value=sessionId, max_age=max_age_sec) resp.set_cookie(key='time', value=expires, max_age=max_age_sec) resp.set_cookie(key='Azone', value="local", max_age=max_age_sec) #LogonCredentials: make_signed_cookie(username, md5(password), seconds=max_age_sec) #LogonCredentials: make_signed_cookie(username, openid/uid, seconds=max_age_sec) return resp else: if SSORequest: return redirect( url_for("login", sso=SSORequest, sso_p=SSOProject, sso_r=SSORedirect, sso_t=SSOToken)) else: return redirect(url_for("login"))
return response @app.route("/") @login_required def index(): return redirect(url_for("ui.index")) @app.errorhandler(404) def not_found(error=None): message = { 'code': 404, 'msg': 'Not Found: ' + request.url, } resp = jsonify(message) resp.status_code = 404 return resp @app.errorhandler(403) def Permission_denied(error=None): message = { "msg": "Authentication failed, permission denied.", "code": 403 } return jsonify(message), 403 if __name__ == '__main__': Host = GLOBAL.get('Host') Port = GLOBAL.get('Port') Debug = GLOBAL.get('Debug', True) app.run(host=Host, port=int(Port), debug=Debug)
#!/usr/bin/python -O #product environment start application with `tornado IOLoop` and `gevent server` from main import app from utils.tool import logger from config import GLOBAL, PRODUCT Host = GLOBAL.get('Host') Port = GLOBAL.get('Port') Environment = GLOBAL.get('Environment') ProcessName = PRODUCT.get('ProcessName') ProductType = PRODUCT.get('ProductType') try: import setproctitle except ImportError, e: logger.warn("%s, try to pip install setproctitle, otherwise, you can't use the process to customize the function" %e) else: setproctitle.setproctitle(ProcessName) logger.info("The process is %s" % ProcessName) try: msg = '%s has been launched, %s:%s' %(ProcessName, Host, Port) print(msg) logger.info(msg) if ProductType == 'gevent': from gevent.wsgi import WSGIServer http_server = WSGIServer((Host, Port), app) http_server.serve_forever() elif ProductType == 'tornado':