Python 错误:[Errno 111]连接在flask-mail中被拒绝

Python 错误:[Errno 111]连接在flask-mail中被拒绝,python,flask,flask-mail,Python,Flask,Flask Mail,我有两个演示,第一个不起作用,但第二个起作用。我想知道为什么 错误: raise err error: [Errno 111] Connection refused \uuuu init\uuuuu.py mail = Mail() MAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 465 MAIL_USE_TLS = False MAIL_USE_SSL= True MAIL_USERNAME = os.environ.get('MAIL_USERNAM

我有两个演示,第一个不起作用,但第二个起作用。我想知道为什么

错误:

raise err
error: [Errno 111] Connection refused
\uuuu init\uuuuu.py

mail = Mail()

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    mail.init_app(app)
    app.debug = True

    return app

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
from ..mymail import mmail

mmail();
from flask.ext.mail import Message
from . import mail

def mmail():
    msg = Message(
      'Hello',
       sender='user@gmail.com',
       recipients=
       ['xxxxxxxxxx@hotmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"
视图.py

mail = Mail()

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    mail.init_app(app)
    app.debug = True

    return app

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
from ..mymail import mmail

mmail();
from flask.ext.mail import Message
from . import mail

def mmail():
    msg = Message(
      'Hello',
       sender='user@gmail.com',
       recipients=
       ['xxxxxxxxxx@hotmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"
mymail.py

mail = Mail()

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    mail.init_app(app)
    app.debug = True

    return app

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
from ..mymail import mmail

mmail();
from flask.ext.mail import Message
from . import mail

def mmail():
    msg = Message(
      'Hello',
       sender='user@gmail.com',
       recipients=
       ['xxxxxxxxxx@hotmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"

奇怪的是,此示例将正常工作:

from flask.ext.mail import Mail, Message
import os

DEBUG = True

MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(__name__)
    mail = Mail(app)

    msg = Message(
      'Hello',
       sender='user@gmail.com',
       recipients=
       ['xxxxxxxxxx@hotmail.com'])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"

app = create_app(os.getenv('FLASK_CONFIG') or 'default')

那么,问题是什么呢?

这里的问题是,你不是在比较苹果和苹果

  • 在非工作示例中,您使用名为
    config
    的未指定字典类对象的值配置应用程序,该对象传入名为
    FLASK\u config
    的环境变量中的键(如果未定义
    FLASK\u config
    ,则返回“默认值”)

  • 在工作示例中,使用当前模块中定义的大写名称配置应用程序:

    app.config.from_object(__name__)
    # config_name is ignored
    
传递到非工作示例中的任何键都提供了一个对象,该对象不具有要用于
邮件的凭据

您可以:

  • 首先通过默认设置配置应用程序,然后从
    config
    字典加载覆盖:

    app.config.from_object(__name__)
    app.config.from_object(config[config_name])
    
  • 或者确保您的配置具有正确的值:

    config = {
        "development": SomeDevConfig,
        "staging": SomeStagingConfig,
        "production": SomeProductionConfig,
        "default": SomeDefaultConfig
    }
    

在第一个示例中,
config
来自哪里?