oswako Posted June 13, 2017 Posted June 13, 2017 Hi I have a flask application that runs perfectly when passing a json package, and returns the same package to the client with no problems but when importing psycopg2 the json package returned is "None" and client complain about the parsing something that is not a json package. Working Code using json library: #!/usr/bin/env pythonimport sys#import psycopg2import jsonfrom flask import Flask,request,jsonify, __version__app = Flask(__name__)application = app @app.route("/")def hello(): return """ HelioHost rules!<br><br> <a href="/flask/python/version/">Python version</a><br> <a href="/flask/flask/version/">Flask version</a> """ @app.route("/python/version/")def p_version(): return "Python version %s" % sys.version @app.route("/flask/version/")def f_version(): return "Flask version %s" % __version__ @app.route("/mother")def mother(): return "Hello world mother" @app.route("/jsonp",methods=['POST'])def jsonp(): webdatajson = request.get_json() # get json package #datajson = jsonify(webdatajson) #make a json package to send back datajson = json.dumps(webdatajson) #makes a json package to send back return(datajson) if __name__ == "__main__": app.run() Another working code using jsonify from flask: #!/usr/bin/env pythonimport sys#import psycopg2import jsonfrom flask import Flask,request,jsonify, __version__app = Flask(__name__)application = app @app.route("/")def hello(): return """ HelioHost rules!<br><br> <a href="/flask/python/version/">Python version</a><br> <a href="/flask/flask/version/">Flask version</a> """ @app.route("/python/version/")def p_version(): return "Python version %s" % sys.version @app.route("/flask/version/")def f_version(): return "Flask version %s" % __version__ @app.route("/mother")def mother(): return "Hello world mother" @app.route("/jsonp",methods=['POST'])def jsonp(): webdatajson = request.get_json() # get json package datajson = jsonify(webdatajson) #make a json package to send back #datajson = json.dumps(webdatajson) #makes a json package to send back return(datajson) if __name__ == "__main__": app.run() NOT WORKING CODE once importing psycopg2: #!/usr/bin/env pythonimport sysimport psycopg2import jsonfrom flask import Flask,request,jsonify, __version__app = Flask(__name__)application = app @app.route("/")def hello(): return """ HelioHost rules!<br><br> <a href="/flask/python/version/">Python version</a><br> <a href="/flask/flask/version/">Flask version</a> """ @app.route("/python/version/")def p_version(): return "Python version %s" % sys.version @app.route("/flask/version/")def f_version(): return "Flask version %s" % __version__ @app.route("/mother")def mother(): return "Hello world mother" @app.route("/jsonp",methods=['POST'])def jsonp(): webdatajson = request.get_json() # get json package #datajson = jsonify(webdatajson) #make a json package to send back datajson = json.dumps(webdatajson) #makes a json package to send back return(datajson) if __name__ == "__main__": app.run() I'm trying to get the json from the client and then store the data into the postgresql that is why i need the psycopg2 adapter. Is this adapter installed or am I doing something wrong? Also for the future when connecting to the postgresql database from the flask app how should I specify the host? ("http://localhost:5432" or "http://ricky.heliohost.org:5432") Thanks in advancedForgot to mention my data username: iotechedomain: ioteche.heliohost.orgserver: Ricky
Krydos Posted June 13, 2017 Posted June 13, 2017 You actually don't have to use a shebang line on flask scripts because only one version of python is available to run flask. /usr/bin/env python is actually python 2.7.5 but like I said the flask.wsgi forces it to use python 3.6.1 and it will just ignore that shebang. You don't need it so you may as well remove it. Not all python modules are installed by default. If you need a module installed, or if you want to check if it's already installed just let us know which version of python you're using, and what server you're on, and what module you need. I installed pyscopg2 for you on Python 3.6.1 on Ricky.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now