[英]Python flask error with app.run
I am new to Python Flask.我是 Python Flask 的新手。 my Flask_app.py able to run if without
app.run()
but shows error when i Put it.如果没有
app.run()
我的 Flask_app.py 能够运行,但是当我放置它时会显示错误。 I currently run in pythonanywhere.com我目前在 pythonanywhere.com 上运行
from flask import Flask,jsonify,abort,make_response
import MySQLdb
import MySQLdb.cursors
app = Flask(__name__)
db = MySQLdb.connect(host='venus.mysql.pythonanywhere-services.com',user='venus',passwd='pw',db='venuspp$default',cursorclass=MySQLdb.cursors.DictCursor)
@app.route('/')
def hello_world():
return 'Hello from bybye!'
@app.route('/KL', methods=['GET'])
def KL():
curs = db.cursor()
try:
curs.execute("SELECT * FROM KL")
a = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
#return "hihi"
return jsonify({'venus': a})
if __name__ == '__main__':
app.run()
The error as below:错误如下:
* Running on http://127.0.0.1:5000/
Traceback (most recent call last):
File "/home/vinus/mysite/flask_app.py", line 49, in <module>
app.run()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 710, in run_simple
inner()
File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()
File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)
File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 410, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "/usr/lib/python3.4/socketserver.py", line 430, in __init__
self.server_bind()
File "/usr/lib/python3.4/http/server.py", line 133, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.4/socketserver.py", line 444, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
Pythonanywhere run flask app through wsgi configuration, so it runs for you, if you check your /var/www/username_pythonanywhere_com_wsgi.py
you will see something like below: Pythonanywhere 通过 wsgi 配置运行 Flask 应用程序,因此它会为您运行,如果您检查您的
/var/www/username_pythonanywhere_com_wsgi.py
您将看到如下内容:
import sys
# add your project directory to the sys.path
project_home = u'/home/username/project_name'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
# import flask app but need to call it "application" for WSGI to work
from yourappmodule import app as application
#app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello!'
so your application will work perfectly without app.run()
所以你的应用程序可以在没有
app.run()
情况下完美app.run()
I would recommend checking: How to Run a Flask Application我建议检查: 如何运行 Flask 应用程序
and case insist into not following the "flask run" path, check: debug Flask server inside Jupyter Notebook并且 case 坚持不遵循“flask run”路径,请检查: 在 Jupyter Notebook 中调试 Flask 服务器
(This material is already here on Stack Overflow, just putting the links here to help who is looking for the answers). (此材料已在 Stack Overflow 上,只是将链接放在这里以帮助正在寻找答案的人)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.