草庐IT

mail_api_flask 接口开发及uwsgi部署项目

QiuPing 2023-03-28 原文

一、项目代码

#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=[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

 

2、创建uwsgi.ini文件

# vim /usr/local/src/mail_api_flask/uwsgi/uwsgi.ini

[uwsgi]
#服务端口号
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.yml

3、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

 

有关mail_api_flask 接口开发及uwsgi部署项目的更多相关文章

  1. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  2. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  3. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  4. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  5. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  6. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  7. ruby-on-rails - ActionController::RoutingError: 未初始化常量 Api::V1::ApiController - 2

    我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc

  8. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  9. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  10. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

随机推荐