如何在Python中安全地存储数据库密码? [英] How to securely store database password in Python?

查看:399
本文介绍了如何在Python中安全地存储数据库密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,保护数据库登录凭据的公认方法是将其存储在Web根目录之外,并使用密码将文件 include()存储。如何将MySQL数据库登录凭据安全地存储在Python应用程序中?

In PHP the accepted way to secure database login credentials is to store them outside the web root, and to include() the files with the passwords. How are MySQL database login credentials safely stored in Python applications?

推荐答案

好吧,一种方法是将密码放入与项目一起部署的单独的config / ini文件。然后,将此文件的路径传递到应用程序的主条目,例如:

Well, one way of doing this is putting the passwords in a separate config/ini file that is not deployed with the project. And then, pass the path of this file to the main entry of the application, e.g.:

python main.py --config = / path /to/config.ini

请注意,您需要解析此-config 参数(请参见argparse ),然后阅读并解析 config.ini 文件。

Note that you'll need to parse this --config argument (see argparse) and then read and parse the config.ini file.

更新
由于您提到了Web应用程序,因此还有另一种方式来传递配置信息-通过环境。例如,如果使用 mod_wsgi ,则可以将其放入wsgi指令中:

Update: Since you mentioned web applications, there is also another way of passing configuration information - through the environ. For example, if you use mod_wsgi, you can putt this in the wsgi directives:

SetEnv my_namespace.some_param some_value

然后,可以通过 os.environ在应用程序中访问此值。

import os
os.environ['my_namespace.some_param']

这篇关于如何在Python中安全地存储数据库密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆