安全的方式来存储API python模块的凭据

I am using a python API module called SpaceTrackTools. However, it requires the script to have the password and username stored as variables in the script. I was wondering if there is a safer way of saving the details?

谢谢。

评论
  • ad_est
    ad_est 回复

    You can encrypt the info using python, and here is how.

  • phic
    phic 回复

    One option is to use dotenv.

    This way you keep your private details in a separate file named .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