在 Python 脚本中隐藏登录凭据 [英] Hide login credentials in Python script

查看:82
本文介绍了在 Python 脚本中隐藏登录凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 2.7.3 并成功地将我的登录凭据传递到 SMTP 服务器,以便在发生事件触发器时向我发送文本或电子邮件.但是,现在我想将 SMTP 服务器登录名和密码存储在一个单独的文件中,以便多个脚本可以使用该信息,或者我可以共享我的脚本,而不必每次都删除我的凭据.

I am using Python 2.7.3 and successfully passing my login credentials to an SMTP server to send me a text or email when an event trigger takes place. However, now I would like to store the SMTP server login and password in a separate file so that multiple scripts could use that information or so that I can share my script without having to remove my credentials each time.

我正在使用很棒的教程来自 Alex Le 关于通过 Python 发送短信.但是现在我想把下面的片段放到另一个可以被多个脚本调用的文件中.这可以只是用户名/密码对,也可以是整个部分.

I am using the great tutorial from Alex Le on sending an SMS via Python. But now I want to take the following segment and put it into another file that can be called by multiple scripts. This could be either just the username/password pair or the whole section.

server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '<gmail_address>', '<gmail_password>' )

我认为自己是一个非常基本的 Python 程序员.我不介意做一些研究,但我想我需要关于我应该寻找哪些术语的帮助.

I would consider myself a pretty basic Python programmer. I don't mind doing some research, but I think I need help on what terms I should be looking for.

推荐答案

从 .yml 文件中获取所有关键变量:

Get all the critical variables from .yml file:

import yaml
conf = yaml.load(open('conf/application.yml'))
email = conf['user']['email']
pwd = conf['user']['password']

server = smtplib.SMTP( "smtp.gmail.com", 587 ) # add these 2 to .yml as well
server.starttls()
server.login(email, pwd)

application.yml 将如下所示:

user:
    email: example@mail.com
    password: yourpassword

这样,您将永远不会在脚本中看到实际凭据.

This way, you will never see the actual credentials in the script.

这篇关于在 Python 脚本中隐藏登录凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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