圆月山庄资源网 Design By www.vgjia.com
文件上传邮件发送
一、原生文件上传
form.html
<img src="/UploadFiles/2021-04-08/17.jpg') }}">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()以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月07日
2024年11月07日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]