为什么我的Dockerized Flask应用程序在mail.send上超时?

时间:2019-07-24 22:39:03

标签: python-3.x flask gunicorn flask-mail

我正在尝试从Gunicorn&Nginx提供的Flask应用发送邮件。在容器中使用mail.send()时,与Flask应用程序的连接超时,在werkzeug的开发服务器上本地运行时可以正常工作。

我尝试将gunicorn工作者类从“同步”更改为“ gevent”,但问题仍然存在。

app.py

from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail

app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile('config.py')

app.wsgi_app = ProxyFix(app.wsgi_app)

db = SQLAlchemy(app)
mail = Mail(app)

import views

views.py

from app import app, mail
from flask_mail import Message
import logging

@app.route('/')
def hello_world():
    email_msg = Message("Subject", sender="mail@mail.com", recipients=["someone@mail.com"])
    email_msg.body = f"Hello!"
    logging.error("Starting email send /request")
    mail.send(email_msg)
    return 'Hello World!'

wsgi.py

from app import app
import logging

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

if __name__ == "__main__":
    app.run()#host='0.0.0.0')

Dockerfile

...

CMD ["gunicorn", "-b", "0.0.0.0:8080", "--worker-class=gevent", "--workers=9","wsgi:app"]

我希望发送电子邮件,但是只是超时了。

Gunicorn输出:

gbcodes    | ERROR:root:Starting email send /request
gbcodes    | ERROR:app:Exception on / [GET]
gbcodes    | Traceback (most recent call last):
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
gbcodes    |     response = self.full_dispatch_request()
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
gbcodes    |     rv = self.handle_user_exception(e)
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
gbcodes    |     reraise(exc_type, exc_value, tb)
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
gbcodes    |     raise value
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
gbcodes    |     rv = self.dispatch_request()
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
gbcodes    |     return self.view_functions[rule.endpoint](**req.view_args)
gbcodes    |   File "/app/views.py", line 13, in hello_world
gbcodes    |     mail.send(email_msg)
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask_mail.py", line 491, in send
gbcodes    |     with self.connect() as connection:
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask_mail.py", line 144, in __enter__
gbcodes    |     self.host = self.configure_host()
gbcodes    |   File "/usr/lib/python3.6/site-packages/flask_mail.py", line 156, in configure_host
gbcodes    |     host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
gbcodes    |   File "/usr/lib/python3.6/smtplib.py", line 1031, in __init__
gbcodes    |     source_address)
gbcodes    |   File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
gbcodes    |     (code, msg) = self.connect(host, port)
gbcodes    |   File "/usr/lib/python3.6/smtplib.py", line 336, in connect
gbcodes    |     self.sock = self._get_socket(host, port, self.timeout)
gbcodes    |   File "/usr/lib/python3.6/smtplib.py", line 1037, in _get_socket
gbcodes    |     self.source_address)
gbcodes    |   File "/usr/lib/python3.6/site-packages/gevent/socket.py", line 96, in create_connection
gbcodes    |     sock.connect(sa)
gbcodes    |   File "/usr/lib/python3.6/site-packages/gevent/_socket3.py", line 335, in connect
gbcodes    |     raise error(err, strerror(err))
gbcodes    | TimeoutError: [Errno 110] Operation timed out

1 个答案:

答案 0 :(得分:0)

关闭我的VPS主机-Scaleway阻止SMTP。通过使用我使用的SMTP服务也支持的端口443进行了修复。