flask :文件上传 flask邮件发送

    Python编程 wuya 161次浏览 已收录 0个评论

    文件上传

    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()

    学海无涯 , 版权所有丨如未注明 , 均为原创丨转载请注明flask :文件上传 flask邮件发送
    喜欢 (0)
    发表我的评论
    取消评论
    表情 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址