美文网首页
配置python和数据库连接

配置python和数据库连接

作者: Colleen_oh | 来源:发表于2019-07-29 11:11 被阅读0次
import base64
import logging,os
from logging.handlers import RotatingFileHandler

def get_logger(file_name,size_MB,backup_count):
    """Parameters:
        file_name - To define the log file name
        size_MB - How munch the size of the log file need to retain
        backup_count - How many backups of the log file need to retain
      Returns:
        Returns a logger of logging
      Raises:
        KeyError - raises an exception"""
    if os.path.isdir('log'):
        filename = os.path.join(os.getcwd(), "log/", file_name)
    else:
        os.mkdir('log')
        filename = os.path.join(os.getcwd(), "log/", file_name)
    maxlogsize = size_MB * 1024 * 1024    #Bytes
    backupcount = backup_count
    format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'
    handler = RotatingFileHandler(filename, mode='a', maxBytes=maxlogsize, backupCount=backupcount)
    formatter = logging.Formatter(format)
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.addHandler(handler)
    return logger


def encode(str):
    utf_str = str.encode(encoding="utf_8")
    return base64.b64encode(utf_str)


def decode(encryp_data):
    utf_byte = base64.b64decode(encryp_data)
    return utf_byte.decode(encoding="utf_8")


_uname = decode("dm9pY2U=")
_upwd = decode("dm9pY2UyMDE5")
_dbhost = "192.000.1.000"#IP
_port = 1234#端口
_dbname = "rootname"#用户名

相关文章

网友评论

      本文标题:配置python和数据库连接

      本文链接:https://www.haomeiwen.com/subject/augvrctx.html