-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (51 loc) · 1.59 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import psycopg2
from configparser import ConfigParser
from flask import Flask
def config(filename='config/database.ini', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
def fetch(sql):
# connect to database listed in database.ini
conn = connect()
cur = conn.cursor()
cur.execute(sql)
# fetch one row
result = cur.fetchone()
print('Closing connection to database...')
cur.close()
conn.close()
return result
def connect():
""" Connect to the PostgreSQL database server and return a cursor """
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# return a conn
return conn
except (Exception, psycopg2.DatabaseError) as error:
print(error)
app = Flask(__name__)
@app.route("/")
def index():
retval = ''
sql = 'SELECT slow_version();'
db_result = fetch(sql)
retval = 'DB Version = ' + ''.join(db_result)
return retval
if __name__ == "__main__": # on running python app.py
app.run() # run the flask app