python login registration

This login registration first goes to QQ to get a verification code

There are specific methods online

see notes

pycharm runs through

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import random
import os
import sys

print(" 登录 注册")
print("1.登录 2.注册")
aa = int(input(""))
if aa==1:
    aaa = int(input("请选择登录方式\n1.邮箱验证码\n2.密码"))
    if aaa==2:
        op1 = str(input("请输入你的邮箱"))
        op11 = int(input("请输入你的密码"))
        f = open(str(op1)+".txt","r")
        a = f.read(6)
        f.close()
        if int(a)==op11:
            print("登录成功!")
    if aaa==1:
        aaaa = input("请输入你的邮箱")
        aaaaa = random.randint(100001,999999)
        # 写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好
        def send_email(msg_from, passwd, msg_to, text_content, file_path=None):
            msg = MIMEMultipart()
            subject = "邮箱验证码"  # 主题
            text = MIMEText(text_content)
            msg.attach(text)

            # file_path = r'read.md'  #如果需要添加附件,就给定路径
            if file_path:  # 最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好
                docFile = file_path
                docApart = MIMEApplication(open(docFile, 'rb').read())
                docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
                msg.attach(docApart)
                print('发送附件!')
            msg['Subject'] = subject
            msg['From'] = msg_from
            msg['To'] = msg_to
            try:
                s = smtplib.SMTP_SSL("smtp.qq.com", 465)
                s.login(msg_from, passwd)
                s.sendmail(msg_from, msg_to, msg.as_string())
                print("发送成功")
            except smtplib.SMTPException as e:
                print("发送失败")
            finally:
                s.quit()


        msg_from = 'xxxxxxxxxxxx'  # 你的邮箱
        passwd = 'xxxxxxxxxxxxxxxxxx'  # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码)
        msg_to = aaaa  # 收件人邮箱,我是自己发给自己
        text_content = "你好,你的验证码是"+str(aaaaa)+",有效期2分钟,如非本人操作,请不要理会。此信息为Pyton助手发送,无需回复。"  # 发送的邮件内容
        file_path = None  # 需要发送的附件目录
        send_email(msg_from, passwd, msg_to, text_content, file_path)
        aaaaaa = int(input("请输入你的验证码"))
        if aaaaaa==aaaaa:
            print("登录成功!")
if aa==2:
    print("请输入你的邮箱")
    ya = str(input(""))
    yaff = int(input("请输入你要设置的密码(6位数字)"))
    yaf = open(ya + ".txt", "w")
    yaf.write(str(yaff))
    yaf.close()
    print("注册成功!")


Guess you like

Origin blog.csdn.net/m0_64036070/article/details/123616944