프로그램을 작성하다보면 서버 접속정보, DB정보 등 변경 가능한 정보들을 소스에 코딩해 놓는 것은 위험하다.
그래서 설정정보를 분리한 .conf나 .ini를 작성하여 외부에서 설정정보를 가지고 오도록 작성하는 것이 좋다.
python의 경우 ConfigParser모듈을 import해서 외부 설정 정보를 가지고 올 수 있다.
예제)
getConfig.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 | #!/usr/bin/env python #-*- codig: utf-8 -*- import ConfigParser def get_config(cfg): host = cfg.get('DB', 'host'), user = cfg.get('DB', 'user'), passwd = cfg.get('DB', 'passwd'), db = cfg.get('DB', 'db'), charset = cfg.get('DB', 'charset'), use_unicode = cfg.getboolean('DB', 'use_unicode') insert_Sql = cfg.get('DB'.'sql_insert') #cfg값 출력 print host, user, passwd, db, charset, use_unicode, insert_Sql if __name__ == '__main__': cfg = ConfigParser.ConfigParser() cfg.read("./DBInfo.conf") #절대주소를 적어주는게 좋다. get_config(cfg) | cs |
DBinfo.conf
[DB]
host= 1.1.1.1
user=ecycle
passwd=ecycle123123
db=testdb
charset=utf8
use_unicode=true
sql_insert = insert into user
(
CONNTIME,
UNO,
UNAME,
UEMAIL,
UPASSWORD
)
VALUES(
str_to_date('%s', '%%Y%%m%%d%%H%%i%%s'),
'%s',
'%s',
'%s',
'%s'
);
'Programming Language > Python' 카테고리의 다른 글
[python] csv 파일 읽기, 쓰기 (0) | 2016.05.16 |
---|---|
[python] 참조 및 발생자 -약한 참조, 반복자(iterator), 발생자(generator) (0) | 2016.05.16 |
[Python]Configparser (외부 설정 정보)사용법 ......(2) - mysql과 python 연동까지 (0) | 2016.05.16 |