프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / WebApi / WebApi.py @ 7f1181fd

이력 | 보기 | 이력해설 | 다운로드 (1.71 KB)

1
# _*_ coding: utf-8 _*_
2

    
3
import sqlite3
4
from flask import Flask
5
app = Flask(__name__)
6

    
7
DATABASE = 'C:\\ProgramData\\Digital PID\\App.db'
8

    
9
def get_db():
10
    db = getattr(Flask, '_database', None)
11
    if db is None:
12
        db = Flask._database = sqlite3.connect(DATABASE)
13
        db.row_factory = make_dicts
14

    
15
    return db
16

    
17
def query_db(query, args=(), one=False):
18
    cur = get_db().execute(query, args)
19
    rv = cur.fetchall()
20
    cur.close()
21
    return (rv[0] if rv else None) if one else rv
22

    
23
def make_dicts(cursor, row):
24
    return dict((cursor.description[idx][0], value)
25
                for idx, value in enumerate(row))
26

    
27

    
28
@app.teardown_appcontext
29
def close_connection(exception):
30
    db = getattr(Flask, '_database', None)
31
    if db is not None:
32
        db.close()
33
        Flask._database = None
34

    
35
    app.logger.debug('close connection')
36

    
37

    
38
@app.route('/')
39
def hello_world():
40
    configs = None
41
    for config in query_db('select * from Configuration'):
42
        if configs is None:
43
            configs = config['Section'] + ',' + config['Key'] + ',' + config['Value']
44
        else:
45
            configs += config['Section'] + ',' + config['Key'] + ',' + config['Value'] + '\n'
46

    
47
    return configs
48

    
49
@app.route('/main')
50
def main():
51
    return 'Main Page'
52

    
53
@app.route('/user/id/<int:userId>')
54
def showUserProfileById(userId):
55
    return 'USER ID : %d' % userId
56

    
57
@app.route('/user/<username>')
58
def showUserProfile(username):
59
    app.logger.debug('RETRIEVE DATA - USER ID : %s' % username)
60
    app.logger.debug('RETRIEVE DATA - Check Compelete')
61
    app.logger.warn('RETRIEVE DATA - Warning... User Not Found.')
62
    app.logger.error('RETRIEVE DATA - ERR! User unauthenification.')
63
    return 'USER : %s' % username
64

    
65
if __name__ == '__main__':
66
    app.run()