While this code is for MySQLdb, it would be similar for most other Python database APIs. The corresponding environment variables, which are all optional:
DB_HOST
DB_PASSWD
DB_PORT
DB_USER
DB_UNIX_SOCKET
While this code is for MySQLdb, it would be similar for most other Python database APIs. The corresponding environment variables, which are all optional:
DB_HOST
DB_PASSWD
DB_PORT
DB_USER
DB_UNIX_SOCKET
import MySQLdb | |
def get_db_kwargs(env): | |
db_kwargs = {} | |
for key in ['host', 'user', 'passwd', 'port', 'unix_socket']: | |
env_var = "DB_{}".format(key.upper()) | |
if env_var in env: | |
db_kwargs[key] = env[env_var] | |
return db_kwargs | |
db_kwargs = get_db_kwargs(os.environ) | |
db = MySQLdb.connect(**db_kwargs) | |
# ... |