如何解决Flask-OSError:[Errno 99]无法分配请求的地址
我正在尝试为我的Web应用程序创建标准的“忘记密码”功能。问题是,当用户键入他们的电子邮件地址并单击提交以接收令牌,然后他们可以更改密码时,页面将一直加载直到出现以下错误消息为止:
This page isn’t working
{{ URL }} didn’t send any data.
ERR_EMPTY_RESPONSE
我了解Flask-Mail会同步发送电子邮件,因此这可能是连接到Gmail的问题。我尝试在配置邮件对象所用的帐户上打开“缺乏安全的应用程序访问权限”,也尝试更改以下配置选项集:
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('EMAIL_USER')
app.config['MAIL_PASSWORD'] = os.environ.get('EMAIL_PASS')
mail = Mail(app)
对此:
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
app.config["MAIL_USERNAME"] = os.environ.get("EMAIL_USER")
app.config["MAIL_PASSWORD"] = os.environ.get("EMAIL_PASS")
mail = Mail(app)
我在网上看到了建议,但没有一个起作用。
以下是相关的路线和方法:
# Generate password reset token unique to user
def get_reset_token(current_user,expires_sec=1800):
s = Serializer(app.config["SECRET_KEY"],expires_sec)
return s.dumps({"user_id": current_user}).decode("utf-8")
# Send reset email
def send_reset_email(user):
rows = db.execute("SELECT * FROM users WHERE id = :current_user",current_user = user)
user_email = rows[0]["email"]
token = get_reset_token(user)
msg = Message("Password Reset Request",sender = "noreply@demo.com",recipients = [user_email])
msg.body = f"""To reset your password,visit the following link:
{url_for('new_password',token = token,_external = True)}
If you did not make this request simply ignore this email and no changes will be made.
"""
mail.send(msg)
@app.route("/reset",methods=["GET","POST"])
def reset():
if request.method == "GET":
return render_template("reset.html")
else:
email = request.form.get("email")
# Check email exists in database
check_email = db.execute("SELECT * FROM users WHERE email = :email",email = email)
if len(check_email) != 1:
return apology("You must enter the email address you registered with",403)
else:
user = check_email[0]["id"]
send_reset_email(user)
flash("An email has been sent with instructions to reset your password")
return render_template("login.html")
在调试方面,我尝试在不使用mail.send(msg)
的情况下运行该应用程序,并且一切正常,减去明显发送的电子邮件,这意味着问题确实与发送的电子邮件有关。另外,我尝试删除token = get_reset_token(user)
函数以查看是否是问题所在,但问题仍然存在。
从终端看来,应用程序在提交电子邮件之后没有问题就可以到达check_email = db.execute("SELECT * FROM users WHERE email = :email",email = email)
,这时它会一直加载直到超时。
ERROR:application:Exception on /reset [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py",line 2447,in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py",line 1952,in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py",line 1821,in handle_user_exception
reraise(exc_type,exc_value,tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py",line 39,in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py",line 1950,in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py",line 1936,in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/pset8/finance/application.py",line 227,in reset
send_reset_email(user)
File "/home/ubuntu/pset8/finance/application.py",line 211,in send_reset_email
mail.send(msg)
File "/home/ubuntu/.local/lib/python3.7/site-packages/flask_mail.py",line 491,in send
with self.connect() as connection:
File "/home/ubuntu/.local/lib/python3.7/site-packages/flask_mail.py",line 144,in __enter__
self.host = self.configure_host()
File "/home/ubuntu/.local/lib/python3.7/site-packages/flask_mail.py",line 156,in configure_host
host = smtplib.SMTP_SSL(self.mail.server,self.mail.port)
File "/usr/local/lib/python3.7/smtplib.py",line 1031,in __init__
source_address)
File "/usr/local/lib/python3.7/smtplib.py",line 251,in __init__
(code,msg) = self.connect(host,port)
File "/usr/local/lib/python3.7/smtplib.py",line 336,in connect
self.sock = self._get_socket(host,port,self.timeout)
File "/usr/local/lib/python3.7/smtplib.py",line 1037,in _get_socket
self.source_address)
File "/usr/local/lib/python3.7/socket.py",line 728,in create_connection
raise err
File "/usr/local/lib/python3.7/socket.py",line 716,in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
INFO:werkzeug:192.168.239.121 - - [15/Oct/2020 18:28:12] "POST /reset HTTP/1.0" 500 -
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。