Guest User

Untitled

a guest
Dec 27th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import pymysql
  2.  
  3.  
  4. # Single
  5. class SingleTon(type):
  6.  
  7. def __init__(self, *args, **kwargs):
  8. self.__instance = None
  9. super().__init__(*args, **kwargs)
  10.  
  11. def __call__(self, *args, **kwargs):
  12. if self.__instance is None:
  13. # 如果 __instance 不存在,创建新的实例
  14. self.__instance = super().__call__(*args, **kwargs)
  15. return self.__instance
  16. else:
  17. # 如果存在,直接返回
  18. return self.__instance
  19.  
  20.  
  21. class MysqlCon(metaclass=SingleTon):
  22. def __init__(self, host, port, user, passwd, database):
  23. self.host = host
  24. self.port = port
  25. self.user = user
  26. self.passwd = passwd
  27. self.databse = database
  28.  
  29. def conn(self):
  30. try:
  31. conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd,
  32. database=self.databse)
  33. except Exception as e:
  34. raise e
  35. return conn.cursor()
  36.  
  37.  
  38. class Spam(metaclass=SingleTon):
  39.  
  40. def __init__(self):
  41. print('Creating Spam')
  42.  
  43.  
  44. def grant_sentence(cursor):
  45. """
  46. data: {}
  47. :return:
  48. """
  49. data = {'database': {'devops': ['auth_group', 'app_service']}, 'user': 'tony',
  50. 'password': '123456', 'ip': '127.0.0.1', 'privileges': ['select', 'update', 'create', 'delete']}
  51. for database in data['database']:
  52. """
  53. grant select,update,create,delete on devops.auth_group to tony@127.0.0.1 identified by 123456 with grant option;
  54. """
  55. for table in data['database'][database]:
  56. grant_sentens = 'grant {privileges} on {database}.{table} to {user}@{database_ip} identified by "{password}" with grant option;'.format(
  57. privileges=','.join(data['privileges']), database=database, table=table,
  58. user=data['user'], database_ip=data['ip'], password=data['password'])
  59. print(grant_sentens)
  60. cursor.execute(grant_sentens)
  61. print('授权完成')
  62.  
  63.  
  64. if __name__ == '__main__':
  65. import sys
  66. # course=conn.conn(host="127.0.0.1", port=3306, user='root', passwd='123456')
  67.  
  68. # print(course)
Add Comment
Please, Sign In to add comment