《Flask Web开发》第六章 电子邮件 error: [Errno 111] Connection refused
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网

《Flask Web开发》第六章 电子邮件 error: [Errno 111] Connection refused

《Flask Web开发》第六章 电子邮件,代码按照书上敲的,邮箱用的是163邮箱
hello.py代码如下:

import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_script import Shell, Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import Form
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from flask_mail import Mail

basedir = os.path.abspathos.path.dirname__file__

app = Flask__name__
app.config[SECRET_KEY] = zhao xiufeng
app.config[SQLALCHEMY_DATABASE_URI] = sqlite:/// + os.path.joinbasedir, data.sqlite
app.config[SQLALCHEMY_COMMIT_ON_TEARDOWN] = True
app.config[SQLALCHEMY_TRACK_MODIFICATIONS] = True
app.config[MAIL_SERVER] = stmp.163.com
app.config[MAIL_PORT] = 465
app.config[MAIL_USE_SSL] = True
app.config[MAIL_USERNAME] = AAA@163.com
app.config[MAIL_PASSWORD] = XXX
mail = Mailapp

manager = Managerapp
bootstrap = Bootstrapapp
moment = Momentapp
db = SQLAlchemyapp
migrate = Migrateapp,db


class Roledb.Model:
    __tablename__ = roles
    id = db.Columndb.Integer, primary_key=True
    name = db.Columndb.String64, unique=True

    users = db.relationshipUser, backref=role, lazy=dynamic

    def __repr__self:
        return <Role %r> % self.name

class Userdb.Model:
    __tablename__ = Users
    id = db.Columndb.Integer, primary_key=True
    username = db.Columndb.String64, unique=True, index=True

    role_id = db.Columndb.Integer, db.ForeignKeyroles.id

    def __repr__self:
        return <User %r> % self.username


class NameFormForm:
    name = StringFieldWhat is your name?, validators=[Required]
    submit = SubmitFieldSubmit


def make_shell_context:
    return dictapp=app, db=db, User=User, Role=Role
manager.add_command"shell", Shellmake_context=make_shell_context
manager.add_commanddb, MigrateCommand  

@app.route/, methods=[GET,POST]
def index:
    form = NameForm
    if form.validate_on_submit:
        user = User.query.filter_byusername=form.name.data.first
        if user is None:
            user = Userusername = form.name.data
            db.session.adduser
            session[known] = False
        else:
            session[known] = True
        session[name] = form.name.data
        form.name.data = 
        return redirecturl_forindex
    return render_templateindex.html, form = form, 
    name = session.getname, known = session.getknown, False

@app.route/user/<name>
def username:
    return render_templateuser.html, name=name

@app.errorhandler404
def page_not_founde:
    return render_template404.html,404

@app.errorhandler500
def internal_server_errore:
    return render_template500.html,500

if __name__ == __main__:
    manager.run

在python shell会话内容:

>>> from flask_mail import Message
>>> from hello import mail
>>> msg = Messagetest subject, sender=AAA@163.com, recipients=[BBB@163.com]
>>> msg.body = text body
>>> msg.html = <b>HTML</b> body
>>> with app.app_context:
...     mail.sendmsg
... 

报错如下:

Traceback most recent call last:
  File "<console>", line 2, in <module>
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 491, in send
    with self.connect as connection:
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host
  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 156, in configure_host
    host = smtplib.SMTP_SSLself.mail.server, self.mail.port
  File "/usr/lib/python2.7/smtplib.py", line 788, in __init__
    SMTP.__init__self, host, port, local_hostname, timeout
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    code, msg = self.connecthost, port
  File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_sockethost, port, self.timeout
  File "/usr/lib/python2.7/smtplib.py", line 793, in _get_socket
    new_socket = socket.create_connectionhost, port, timeout
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused

不知道这是神马错误?
谢谢所有点进来的大神!!!

已经发现问题了,app.config[MAIL_SERVER] = stmp.163.com写错了,正确的是app.config[MAIL_SERVER] = smtp.163.com

栏目列表

广告位

澳门银河官网