0

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?
8
  • 2
    Another process(maybe another script running) uses exact port you try to run your app Commented Dec 30, 2016 at 14:13
  • without app.run(), any problem with my script? Commented Dec 30, 2016 at 14:17
  • Try to use 'app.run(port=8080)' or something else, maybe 8000 is in use. Commented Dec 30, 2016 at 14:17
  • No I think the only prob is port you are using for your app Commented Dec 30, 2016 at 14:18
  • 1
    on pythonanywhere you must not use app.run(). There's a help page about it here: help.pythonanywhere.com/pages/Flask Commented Jan 6, 2017 at 12:31

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.