I have two scripts I'm working on which use a MYSQL DB to set error logging and storage , And I would like to have a way to store mysql credentials in the script and use it in different locations, but I do not want to open the connection (open it once in the beginning of the script). Is this the best practice or 'dragon' method to do this?
class MyDB: def __init __ (sql_server, user, password): self.server = sql_server self.user = user self .password = password def __getattr __ (self, key): conn = MYSQLDB.connect (self.server, self.user, self.password) Val = getattr (conn, key) conn.close () return value settings.py
db = MyDb (my_server, my_user, my_pass) other_script.py DB db.whatever () # import from settings or import setting settings.db.do_something () Maybe something like that ... and just around that object
Pass the problem, you still have the server in your program Credentials will need to store. There are a few ways to obscure it but none is good.
Use a simple encoding like A base 64 (not really encrypted, just encoded)
B Use some type of encryption libraries It is encrypted and the method According to it is very difficult to crack. Unfortunately you still need the key in the program (see A or B to encrypt the key)
No comments:
Post a Comment