flask入门之文件上传与邮件发送示例

yipeiwu_com5年前Python基础

文件上传邮件发送

一、原生文件上传

form.html

  <img src="{{ url_for('static',filename='img/17.jpg') }}" width="300" alt="">
  <form action="" method="post" enctype="multipart/form-data">
    <p>修改头像 <input type="file" name="file"></p>
    <p><input type="submit" value="提交"></p>
  </form>

manage.py

#文件上传的视图函数
@app.route('/upload/',methods=['GET','POST'])
def icon():
  img_name = None
  if request.method == 'POST' and 'file' in request.files:
    # return '有文件上传了'
    file = request.files.get('file')
    filename = file.filename #拿到文件名称
    #图片上传保存的路径
    imgPath = os.path.join(os.getcwd(), 'static/upload/'+filename)
    file.save(imgPath)
    img_name = filename
  return render_template('user/change_icon.html',img_name=img_name)

使用wtf和bootstrap渲染文件上传

from flask import Flask,render_template,request
from flask_script import Manager
from flask_wtf import FlaskForm
from flask_wtf.file import FileField,FileAllowed,FileRequired
from wtforms import SubmitField
from flask_uploads import UploadSet,patch_request_class,configure_uploads,IMAGES
from flask_bootstrap import Bootstrap
import os
from PIL import Image

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64
app.config['SECRET_KEY'] = 'abcdef'
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')
bootstrap = Bootstrap(app)
file = UploadSet('photos',IMAGES)
configure_uploads(app,file)
patch_request_class(app,size=None)
manager = Manager(app)

#自定义一个文件上传的表单类
class File(FlaskForm):
  photos = FileField('修改头像',validators=[FileRequired(message='文件不能为空'),FileAllowed(file,message='该文件类型不允许上传')])
  submit = SubmitField('提交')

@app.route('/')
def index():
  return render_template('index.html')

#生成随机的图片名称
def random_name(suffix,length=32):
  import string,random
  myStr = string.ascii_letters + '0123456789'
  return ''.join(random.choice(myStr) for i in range(length))+suffix

@app.route('/upload/',methods=['GET','POST'])
def icon():
  form = File()
  img_url = None
  if form.validate_on_submit():
    data = request.files.get('photos')
    suffix = os.path.splitext(data.filename)[-1]
    newName = random_name(suffix)
    file.save(data,name=newName)
    img_url = file.url(newName)

    img = Image.open(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],newName))
    print(img.size) # 获取图片大小
    # 设置尺寸
    img.thumbnail((128, 128)) # 当前缩放不是等比缩放 变成等比缩放
    img.save(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],'s_'+newName))
  return render_template('user/wtf-uplods.html',form=form,img_url=img_url)

if __name__ == '__main__':
  manager.run()

模板中的代码

from flask import Flask,render_template,request
from flask_script import Manager
from flask_wtf import FlaskForm
from flask_wtf.file import FileField,FileAllowed,FileRequired
from wtforms import SubmitField
from flask_uploads import UploadSet,patch_request_class,configure_uploads,IMAGES
from flask_bootstrap import Bootstrap
import os
from PIL import Image

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64
app.config['SECRET_KEY'] = 'abcdef'
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')
bootstrap = Bootstrap(app)
file = UploadSet('photos',IMAGES)
configure_uploads(app,file)
patch_request_class(app,size=None)
manager = Manager(app)

#自定义一个文件上传的表单类
class File(FlaskForm):
  photos = FileField('修改头像',validators=[FileRequired(message='文件不能为空'),FileAllowed(file,message='该文件类型不允许上传')])
  submit = SubmitField('提交')

@app.route('/')
def index():
  return render_template('index.html')

#生成随机的图片名称
def random_name(suffix,length=32):
  import string,random
  myStr = string.ascii_letters + '0123456789'
  return ''.join(random.choice(myStr) for i in range(length))+suffix

@app.route('/upload/',methods=['GET','POST'])
def icon():
  form = File()
  img_url = None
  if form.validate_on_submit():
    data = request.files.get('photos')
    suffix = os.path.splitext(data.filename)[-1]
    newName = random_name(suffix)
    file.save(data,name=newName)
    img_url = file.url(newName)

    img = Image.open(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],newName))
    print(img.size) # 获取图片大小
    # 设置尺寸
    img.thumbnail((128, 128)) # 当前缩放不是等比缩放 变成等比缩放
    img.save(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],'s_'+newName))
  return render_template('user/wtf-uplods.html',form=form,img_url=img_url)

if __name__ == '__main__':
  manager.run()

二、发送邮件 flask-mail

pip install flask-mail

设置临时环境变量

windows set 名=值

Ubuntu下 export 名=值

注意: 名和值都不用加引号

单线程发送邮件

from flask import Flask,render_template
from flask_script import Manager
from flask_mail import Mail,Message
import os

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.163.com'
# 为了保密 将邮箱账号和授权码都加入到了临时环境变量中
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

mail = Mail(app)
manager = Manager(app)

@app.route('/send_mail/')
def send_mail():
  msg = Message(subject='大郎',recipients=['793390457@qq.com'],sender=app.config['MAIL_USERNAME'])
  msg.html = render_template('email/activate.html',username='大郎')
  mail.send(message=msg)
  return '发送邮件'

if __name__ == '__main__':
  manager.run()

异步发送邮件

from flask import Flask,render_template
from flask_script import Manager
from flask_mail import Mail,Message
import os
from threading import Thread

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.163.com'
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

mail = Mail(app)
manager = Manager(app)

@app.route('/send_mail/')
def send_mail():
  msg = Message(subject='大郎',recipients=['793390457@qq.com'],sender=app.config['MAIL_USERNAME'])
  msg.html = render_template('email/activate.html',username='大郎')
  thr = Thread(target=async_send_mail,args=(msg,)) #创建线程 参数1为创建子线程 参数2为传递参数 类型为元组
  thr.start() #开启线程
  return '发送邮件'

def async_send_mail(msg):
  #开启程序上下文 把当前请求 作为同一个请求
  with app.app_context():
    mail.send(message=msg)

if __name__ == '__main__':
  manager.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django中模版的子目录与include标签的使用方法

get_template()中使用子目录 把所有的模板都存放在一个目录下可能会让事情变得难以掌控。 你可能会考虑把模板存放在你模板目录的子目录中,这非常好。 事实上,我们推荐这样做;一些...

Python中生成一个指定长度的随机字符串实现示例

方法一: 定义一个函数,参数为所要生成随机字符串的长度。通过random.randint(a, b)方法得到随机数字,具体函数如下: def generate_random_str(...

Python利用神经网络解决非线性回归问题实例详解

Python利用神经网络解决非线性回归问题实例详解

本文实例讲述了Python利用神经网络解决非线性回归问题。分享给大家供大家参考,具体如下: 问题描述 现在我们通常使用神经网络进行分类,但是有时我们也会进行回归分析。 如本文的问题: 我...

Python中http请求方法库汇总

最近在使用python做接口测试,发现python中http请求方法有许多种,今天抽点时间把相关内容整理,分享给大家,具体内容如下所示: 一、python自带库----urllib2...

python之拟合的实现

python之拟合的实现

一、多项式拟合 多项式拟合的话,用的的是numpy这个库的polyfit这个函数。那么多项式拟合,最简单的当然是,一次多项式拟合了,就是线性回归。直接看代码吧 import nump...