python--getpass密码非明文显示

2019-10-10  本文已影响0人  小二哥很二

我们在执行input('请输入你的密码:')时候,会显示明文,而getpass就解决了明文显示的问题:

import getpass
import smtplib
from email.mime.text import MIMEText

class Sendmain(object):
    def __init__(self,fromuser,pwd,touser,title,content,host='smtp.sina.cn',port=25):
        self.touser=touser
        self.content=content
        self.title=title
        self.host=host
        self.port=port
        self.fromuser=fromuser
        self.pwd=pwd

    def send_main(self):
        msg=MIMEText(self.content,'plain','utf-8')
        msg["Subject"]=self.title
        msg['From']=self.fromuser
        msg['To']=self.touser

        try:
            smtp=smtplib.SMTP(self.host,self.port)
            smtp.login(self.fromuser,self.pwd)
            smtp.sendmail(self.fromuser,self.touser,msg.as_string())
            smtp.quit()
            print('邮件发送成功')

        except smtplib.SMTPException as e:
            print(e)

if __name__ == '__main__':
    fromuser=input('请输入发送人账号:')
    pwd=getpass.getpass("输入你的密码:")
    touser='xxxxxxxxx@qq.com'
    title='密码练习'
    content='getpass练习'
    send=Sendmain(fromuser,pwd,touser,title,content)
    send.send_main()

执行结果入图:

image.png

注意:必须要在终端打开执行.py文件,getpass才能生效

上一篇 下一篇

猜你喜欢

热点阅读