Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- # Single
- class SingleTon(type):
- def __init__(self, *args, **kwargs):
- self.__instance = None
- super().__init__(*args, **kwargs)
- def __call__(self, *args, **kwargs):
- if self.__instance is None:
- # 如果 __instance 不存在,创建新的实例
- self.__instance = super().__call__(*args, **kwargs)
- return self.__instance
- else:
- # 如果存在,直接返回
- return self.__instance
- class MysqlCon(metaclass=SingleTon):
- def __init__(self, host, port, user, passwd, database):
- self.host = host
- self.port = port
- self.user = user
- self.passwd = passwd
- self.databse = database
- def conn(self):
- try:
- conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd,
- database=self.databse)
- except Exception as e:
- raise e
- return conn.cursor()
- class Spam(metaclass=SingleTon):
- def __init__(self):
- print('Creating Spam')
- def grant_sentence(cursor):
- """
- data: {}
- :return:
- """
- data = {'database': {'devops': ['auth_group', 'app_service']}, 'user': 'tony',
- 'password': '123456', 'ip': '127.0.0.1', 'privileges': ['select', 'update', 'create', 'delete']}
- for database in data['database']:
- """
- grant select,update,create,delete on devops.auth_group to tony@127.0.0.1 identified by 123456 with grant option;
- """
- for table in data['database'][database]:
- grant_sentens = 'grant {privileges} on {database}.{table} to {user}@{database_ip} identified by "{password}" with grant option;'.format(
- privileges=','.join(data['privileges']), database=database, table=table,
- user=data['user'], database_ip=data['ip'], password=data['password'])
- print(grant_sentens)
- cursor.execute(grant_sentens)
- print('授权完成')
- if __name__ == '__main__':
- import sys
- # course=conn.conn(host="127.0.0.1", port=3306, user='root', passwd='123456')
- # print(course)
Add Comment
Please, Sign In to add comment