A very simple Flask Hello World app for you to get started with...
from flask import Flask,jsonify,abort,make_response
import MySQLdb
import MySQLdb.cursors
app = Flask(__name__)
db = MySQLdb.connect(host='vinus.mysql.pythonanywhere-services.com',user='vinus',passwd='pw',db='vinuspp$default',cursorclass=MySQLdb.cursors.DictCursor)
@app.route('/')
def hello_world():
return 'Hello from vee!'
@app.route('/KLSE', methods=['GET'])
def KLSE():
curs = db.cursor()
try:
curs.execute("SELECT * FROM KLSE")
a = curs.fetchall()
except Exception:
return 'Error: unable to fetch items'
#return "hihi"
return jsonify({'Stock': a})
if __name__ == '__main__':
app.run()
read that pythonanywhwre need to add if __name__ == '__main__':
but unable to run.
* 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