在 Flask 中不断收到对 app.run() 的回溯调用

Keep getting traceback call for app.run() in Flask

所以我正在使用 Flask 进行这个项目,但我对使用 Flask 还是个新手。但是,每当我尝试 运行 我用于该项目的脚本时,我都会收到一个抱怨 app.run().

的回溯调用

这是我脚本中的代码:

import os
from flask import Flask, render_template, request, flash, redirect, url_for
from flask.ext.assets import Environment, Bundle
from flask_wtf import Form 
from wtforms import TextField, TextAreaField, SubmitField
from wtforms.validators import InputRequired

CSRF_ENABLED = True
app = Flask(__name__)
app.secret_key = 'development key'
assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle('assets/scss/app.scss', filters='scss', output='css/app.css')
assets.register('app_scss', scss)

@app.route('/')
def server_1():
     return render_template('server_1.html')


if __name__ == '__main__':
      app.run()

这是脚本不断输出的回溯(我将各个回溯隔开以使其更易于阅读):

$python app.py
 * Running on http://127.0.0.1:5000/
 Traceback (most recent call last):
 File "app.py", line 22, in <module>
   app.run()

 File "/Library/Python/2.7/site-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-  py2.7.egg/werkzeug/serving.py", line 710, in run_simple
 inner()

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 410, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)

socket.error: [Errno 48] Address already in use

我不太确定我做错了什么,所以非常感谢您提供的任何帮助。

最终错误:

socket.error: [Errno 48] Address already in use

表示您已经在 127.0.0.1 端口 5000 上 运行ning 了其他东西。

要找出哪个进程正在使用此地址,您可以运行以下命令(在OS X 上)

lsof -i tcp:5000

或者,如果您希望使用不同的端口,例如 5001,请调整您的代码:

if __name__ == '__main__':
      app.run(port=5001)