mail_api_flask 接口开发及uwsgi部署项目
一、项目代码#vim /usr/local/src/mail_api_flask/run.py
"""
mail_api_flask 为基于Flask web框架开发的在线发送邮件api,实现功能复用。支持html模板邮件。
"""
from flask import Flask
from flask import request
from flask_mail import Mail, Message
from concurrent.futures import ThreadPoolExecutor# 线程池
import time
executor = ThreadPoolExecutor(max_workers=10)# max_workers 配置最大线程数
app = Flask(__name__)
def sendmail(subject, sender_name, recipients, html):
"""
发送邮件
:param subject: 邮件主题
:param sender_name: 发件人别名
:param recipients: 接收邮箱,list格式
:param html: html内容模板
:return: 状态
"""
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'xxxxx@foxmail.com'
app.config['MAIL_PASSWORD'] = 'xxxxx'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
print("sendmail is running..." + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
try:
msg = Message(subject=subject, sender=(sender_name, 'xxxxx@foxmail.com'), recipients=recipients)
msg.html = html
executor.submit(mail.send(msg))# 多线程发送邮件
status = {"msg": "Successed", "code": "1000", "sendtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
print("邮件已成功发送到【%s】" % recipients + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
except Exception as e:
status = {"msg": e, "code": "1001", "sendtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
print("邮件未发送到【%s】" % recipients + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
return status
# http://127.0.0.1:5000/api/v1/sendmail
@app.route("/api/v1/sendmail", methods=["GET", "POST"])
def index():
if request.method == "POST":
subject = request.values.get("subject")
sender_name = request.values.get("sender_name")
recipients = request.values.get("recipients")
html = request.values.get("html")
return sendmail(subject=subject, sender_name=sender_name, recipients=, html=html)
return "sendmail接口运行正常!" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')
二、uwsgi部署项目
1、创建虚拟环境
cd /usr/local/src/pyENV/
virtualenv mail_api_flask_EN<br><br>
2、创建uwsgi.ini文件
# vim /usr/local/src/mail_api_flask/uwsgi/uwsgi.ini
#服务端口号
http = 127.0.0.1:5000
#项目路径
chdir= /usr/local/src/mail_api_flask
#wsgi文件 run就是flask启动文件去掉后缀名 app是run.py里面的Flask对象
module= run:app
virtualenv =/usr/local/src/pyENV/mail_api_flask_ENV
wsgi-file = /usr/local/src/mail_api_flask/run.py
pidfile = /usr/local/src/mail_api_flask/master.pid
#指定工作进程
processes= 4
#主进程
master = true
#每个工作进程有2个线程
threads = 2
#指的后台启动 日志输出的地方
daemonize = /usr/local/src/mail_api_flask/uwsgi/logs/uwsgi.log
#保存主进程的进程号
pidfile = master.pid
#服务启动的参数
pyargv = -f web_etl.yml3、uwsgi 启动 停止 重新启动 命令
#启动
uwsgi --ini uwsgi.ini
#停止
uwsgi --stop master.ini
#重新启动
uwsgi --reload master.ini
4、创建start_project.sh文件,赋予可执行权限
#chmod +x start_project.sh
#vim /usr/local/src/mail_api_flask/uwsgi/start_project.sh
uwsgi --ini /usr/local/src/mail_api_flask/uwsgi/uwsgi.ini
5、编辑nginx配置文件
# sendmail api
server {
listen 8082;
server_name_;
location =/sendmail {
#include /etc/nginx/uwsgi_params;
proxy_pass http://127.0.0.1:5000/api/v1/sendmail;
}
}
6、设置开机自启动
#赋予rc.local可执行权限
chmod +x /etc/rc.d/rc.local
#在rc.local添加启动脚本路径
/usr/local/src/mail_api_flask/uwsgi/start_project.sh
#使rc.local文件生效
source /etc/rc.d/rc.local
来源:https://www.cnblogs.com/QiuPing-blog/p/17240722.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]