为 API python 模块存储凭据的安全方式
Secure Way to Store Credentials for an API python module
我正在使用名为 SpaceTrackTools 的 python API
模块。但是,它要求脚本将密码和用户名存储为脚本中的变量。我想知道是否有更安全的保存细节的方法?
谢谢。
一种选择是使用 dotenv。
这样您就可以将您的私人详细信息保存在一个名为 .env
.
的单独文件中
然后您在 运行 时间阅读脚本中的私人详细信息。
例如:
文件 .env 值:
USERNAME=MyUserName
PASSWORD=Spec!alP@ssw0rd*19?
脚本用法:
import os # also need os
from dotenv import load_dotenv
load_dotenv() # blank if .env file in same directory as script
# load_dotenv('<path to file>.env') to point to another location
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
# your code
您可以使用 keyring 在脚本之外存储您的用户名和密码。命令行界面允许您获取、设置或删除存储的密码。您可以像存储密码一样存储您的用户名,以确保其安全。
$ keyring --help
Usage: keyring [get|set|del] SERVICE USERNAME
Options:
-h, --help show this help message and exit
-p KEYRING_PATH, --keyring-path=KEYRING_PATH
Path to the keyring backend
-b KEYRING_BACKEND, --keyring-backend=KEYRING_BACKEND
Name of the keyring backend
--list-backends List keyring backends and exit
--disable Disable keyring and exit
从命令行设置服务的用户名和密码。
$ keyring set spacetracktools username
Password for 'username' in 'spacetracktools':
$ keyring set spacetracktools password
Password for 'password' in 'spacetracktools':
import keyring
# get username and password from keyring
username = keyring.get_password("spacetracktools", "username")
password = keyring.get_password("spacetracktools", "password")
print("My username is:", username)
print("My password is:", password)
添加到 .env 方法中,请确保您设置了 .gitignore 文件,这样 git 就不会在 repo 中包含 .env 文件。您可以在 Github gitignore repo here
找到 python 的 .gitignore 文件
我正在使用名为 SpaceTrackTools 的 python API
模块。但是,它要求脚本将密码和用户名存储为脚本中的变量。我想知道是否有更安全的保存细节的方法?
谢谢。
一种选择是使用 dotenv。
这样您就可以将您的私人详细信息保存在一个名为 .env
.
然后您在 运行 时间阅读脚本中的私人详细信息。
例如:
文件 .env 值:
USERNAME=MyUserName
PASSWORD=Spec!alP@ssw0rd*19?
脚本用法:
import os # also need os
from dotenv import load_dotenv
load_dotenv() # blank if .env file in same directory as script
# load_dotenv('<path to file>.env') to point to another location
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
# your code
您可以使用 keyring 在脚本之外存储您的用户名和密码。命令行界面允许您获取、设置或删除存储的密码。您可以像存储密码一样存储您的用户名,以确保其安全。
$ keyring --help
Usage: keyring [get|set|del] SERVICE USERNAME
Options:
-h, --help show this help message and exit
-p KEYRING_PATH, --keyring-path=KEYRING_PATH
Path to the keyring backend
-b KEYRING_BACKEND, --keyring-backend=KEYRING_BACKEND
Name of the keyring backend
--list-backends List keyring backends and exit
--disable Disable keyring and exit
从命令行设置服务的用户名和密码。
$ keyring set spacetracktools username
Password for 'username' in 'spacetracktools':
$ keyring set spacetracktools password
Password for 'password' in 'spacetracktools':
import keyring
# get username and password from keyring
username = keyring.get_password("spacetracktools", "username")
password = keyring.get_password("spacetracktools", "password")
print("My username is:", username)
print("My password is:", password)
添加到 .env 方法中,请确保您设置了 .gitignore 文件,这样 git 就不会在 repo 中包含 .env 文件。您可以在 Github gitignore repo here
找到 python 的 .gitignore 文件