我有以下代码:
username = 'username@gmail.com'
password = 'password'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
我正在寻找一种不用硬编码密码的方法。 GETPASS需要交互式密码提示,我不想要它。 有没有其他方法可以实现这一目标?
提前致谢
答案 0 :(得分:0)
有很多方法可以进行模糊处理或加密,但是您遇到的问题是,无论它是什么,如果您的代码中有人有权访问您的代码,可以阅读它,可以解密它。
我建议将它以加密的base64编码形式存储在数据库中。 Base64编码基本上意味着你不会得到你的数据库可能会讨厌的任何奇怪的字符。
这是一个例程,可以为您加密和解密AES
import base64
from Crypto.Cipher import AES
key ="" # ASSIGN 32 ALPHANUMERIC CHARACTERS TO THIS VAR
iv = "" # ASSIGN 16 ALPHANUMERIC CHARACTERS TO THIS VAR
def enCrypt(string):
"""
Encypts in AES 256bit and then base64 encodes it for database entry
"""
encFormat = AES.new(key, AES.MODE_CFB, iv)
return base64.b64encode(encFormat.encrypt(string))
def deCrypt(string):
"""
Decodes the string fron base64 then decrypt the resulting AES string
"""
destring = base64.b64decode(string)
encFormat = AES.new(key, AES.MODE_CFB, iv)
return encFormat.decrypt(destring)
这是相当自我解释的,但如果你有一个密码,如" bobbins"
encrypted = enCrypt("bobbins")
并解密
deCrypt(encrypted)
但我强烈建议存储在数据库中,即使它像sqlite一样小而基本。为此,我建议查看SQLAlchemy模块
答案 1 :(得分:0)
私人公钥尝试怎么样?
使用您的公钥加密密码,并在程序中编写硬编码。 将私钥保存在计算机上定义的某个位置(不进行任何操作或共享任何类型)。 然后,要在程序中登录,请使用您的私钥加密密码。
它只能在你的电脑上工作(你有私钥),但也许这对你来说足够了。
看看PyCrypto:https://pythonhosted.org/pycrypto/
答案 2 :(得分:0)
这一切都取决于你在这里得到的东西 - 如果它只是不想分享密码你可以设置一个“特定于应用程序”的密码,但这仍然允许其他人访问你的帐户......
OAuth允许您设置一个范围,使得拥有“密码”的人可以发送电子邮件,但不能访问其他帐户活动,甚至不能阅读邮件,具体取决于您的意愿。
如果您未设置SMTP,则Google每天可通过GMail API提供1,000,000,000个“配额单元”。
您可以使用OAuth身份验证通过 https://www.googleapis.com/auth/gmail.send “scope”发送电子邮件。
如果您决定使用python library这条路线,您应该可以使用Google的示例发送消息:
"""Send an email message from the user's account.
"""
import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os
from apiclient import errors
def SendMessage(service, user_id, message):
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print 'Message Id: %s' % message['id']
return message
except errors.HttpError, error:
print 'An error occurred: %s' % error
def CreateMessage(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def CreateMessageWithAttachment(sender, to, subject, message_text, file_dir,
filename):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
file_dir: The directory containing the file to be attached.
filename: The name of the file to be attached.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEMultipart()
message['to'] = to
message['from'] = sender
message['subject'] = subject
msg = MIMEText(message_text)
message.attach(msg)
path = os.path.join(file_dir, filename)
content_type, encoding = mimetypes.guess_type(path)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
fp = open(path, 'rb')
msg = MIMEText(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(path, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(path, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
else:
fp = open(path, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
return {'raw': base64.urlsafe_b64encode(message.as_string())}