您的位置 首页 python学习笔记

flask :文件上传 flask邮件发送

文件上传 from flask import Flask, request, render_template, url_for, send_from_directory …

文件上传

from flask import Flask, request, render_template, url_for, send_from_directory
from flask_script import Manager
import os

app = Flask(__name__)
manager = Manager(app)
app.config["UPLOAD_FOLDER"] = os.path.join(os.getcwd(), "static/upload")

#图片显示的路由
@app.route("/upload/<filename>")

def uploaded(filename):
    return send_from_directory(app.config["UPLOAD_FOLDER"], filename=filename)
@app.route("/upload/", methods=["POST", "GET"])
def upload():
    img_url = None
    if request.method == "POST":
        #获取上传图像的对象
        photo = request.files.get("photo")
        print(photo)
        #meinv.jpg

        print(photo.filename)
        if photo:
            #保存起来,
            pathname= os.path.join(app.config["UPLOAD_FOLDER"], photo.filename)
            #b保存上传文件
            photo.save(pathname)
            #构造这个img_url
            img_url = url_for("uploaded", filename = photo.filename)
            print(img_url)
        else:
            return "上传失败"

    return render_template("upload.html", img_url=img_url)
if __name__ == "__main__":
    manager.run()
    
    hsjsjsjm.nsjasas.jpg
  • 邮件发送
    from flask import Flask
    from flask_script import Manager
    #pip install flask-mail
    from flask_mail import Mail,Message
    import os
    
    
    app = Flask(__name__)
    manager = Manager(app)
    
    #邮件发送需要配置一些东西
    app.config["MAIL_SERVER"] = "smtp.1000phone.com"
    #用户名
    app.config["MAIL_USERNAME"] = "wangbo3@1000phone.com"
    #密码
    app.config["MAIL_PASSWORD"] = os.getenv("MAIL_PASSWORD", "123456")
    #实例化发送邮件对象
    mail = Mail(app)
    @app.route("/send/")
    def send_mail():
        msg = Message("账户激活",recipients=["1486728869@qq.com"], sender=app.config["MAIL_USERNAME"])
        msg.html="恭喜你中奖了"
        mail.send(msg)
        return "邮件已发送"
    
    if __name__ == "__main__":
        manager.run()
本文来自网络,不代表立场,转载请注明出处:http://www.zhengzhou888seo.com/3828.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

联系我们

18134416366

在线咨询: QQ交谈

邮箱: 894974231@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部