Python flask error with app.run

9.1k views Asked by At

I am new to Python Flask. my Flask_app.py able to run if without app.run() but shows error when i Put it. I currently run in 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
  1. Is my script correct and robust if without app.run()? I will update my database daily. I do not want it to break.
  2. What shall I do to correct the error?
2

There are 2 answers

0
metmirr On BEST ANSWER

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:

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()

0
ChrCury78 On

I would recommend checking: How to Run a Flask Application

and case insist into not following the "flask run" path, check: debug Flask server inside Jupyter Notebook

(This material is already here on Stack Overflow, just putting the links here to help who is looking for the answers).