Guest User

Untitled

a guest
Feb 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. from flask import Flask, g, jsonify
  2. import werkzeug, os
  3. from werkzeug.utils import secure_filename
  4.  
  5. import psycopg2
  6. from psycopg2 import pool
  7.  
  8. def get_db():
  9. print ('GETTING CONN')
  10. if 'db' not in g:
  11. g.db = app.config['postgreSQL_pool'].getconn()
  12. return g.db
  13.  
  14.  
  15. def create_app():
  16. app = Flask(__name__)
  17.  
  18. app.config['postgreSQL_pool'] = psycopg2.pool.SimpleConnectionPool(1, 20,user = "postgres",
  19. password = "top_secret",
  20. host = "127.0.0.1",
  21. port = "9502",
  22. database = "postgres")
  23.  
  24. @app.teardown_appcontext
  25. def close_conn(e):
  26. print('CLOSING CONN')
  27. db = g.pop('db', None)
  28. if db is not None:
  29. app.config['postgreSQL_pool'].putconn(db)
  30.  
  31.  
  32. @app.route('/')
  33. def index():
  34. print ('ROUTE')
  35. db = get_db()
  36. cursor = db.cursor()
  37.  
  38. cursor.execute("select 1;")
  39. result = cursor.fetchall()
  40. print (result)
  41.  
  42. cursor.close()
  43. return jsonify(result)
  44.  
  45. return app
  46.  
  47. if __name__ == '__main__':
  48. app = create_app()
  49. app.run(debug=True)
Add Comment
Please, Sign In to add comment