티스토리 뷰

5. 파이썬

REST API, Database Connection, os.path.dirname(__file__)

패스트코드블로그 2020. 10. 5. 08:55

config.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
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
 
 
import mysql.connector
 
basedir = os.path.dirname(os.path.abspath(__file__))
 
 
db = {
    'user' : 'root',
    'password' : 'root',
    'host' : 'localhost',
    'port' : '3306',
    'database' : 'mariadb'
}
 
 
mysql_con = None
 
#------------------------------------------------------#
def query_executor(cursor):
    sql = "select * from food"
    cursor.execute(sql,)
#------------------------------------------------------#
 
 
if __name__ == "__main__":
 
    print('test')
    try:
 
        mysql_con = mysql.connector.connect(host='localhost', port='3306', database='mariadb', user='root', password='root')
                                            
        mysql_cursor = mysql_con.cursor(dictionary=True)
 
        query_executor(mysql_cursor)
 
        for row in mysql_cursor:
            print('price is: '+str(row['price']))
        mysql_cursor.close()
 
 
 
    except Exception as e:
        print(e.message)
 
 
    finally:
        if mysql_con is not None:
            mysql_con.close()
cs

 

app.py - SOUP

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
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
 
from flask import Flask, render_template, request
from service.calculator_service import CalculatorService
 
from config import basedir
 
app = Flask(__name__)
 
@app.route('/')
def index():
    return render_template('index.html')
 
@app.route('/calc', methods=["post"])
def calc():
    num1 = request.form['num1']
    num2 = request.form['num2']
    opcode = request.form['opcode']
    calc = CalculatorService()
    result = calc.calc(num1, num2, opcode)
    render_params = {}
    render_params['result'= result
    return render_template('index.html'**render_params)
 
 
if __name__ == '__main__':
    print(f'******* {basedir} ')
    app.run()
cs

 

app.py - RESTful

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from flask import Flask
from flask_restful import Resource, Api
 
app = Flask(__name__)
api = Api(app)
 
class Rest(Resource):
    def get(self):
        return {'rest''Good !'}
 
api.add_resource(Rest, '/')
 
if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=8080)
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함