-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
59 lines (49 loc) · 1.36 KB
/
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
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'TT'
import sys,os,time
try:
from configparser import ConfigParser
except:
from ConfigParser import ConfigParser
def read_config(config_file_path, field, key):
cf = ConfigParser()
try:
cf.read(config_file_path)
result = cf.get(field, key)
except:
sys.exit(1)
return result
def write_config(config_file_path, field, key, value):
cf = ConfigParser()
try:
cf.read(config_file_path)
cf.set(field, key, value)
cf.write(open(config_file_path,'w'))
except:
sys.exit(1)
return True
class Config():
def __init__(self, path):
self.path = path
self.cf = ConfigParser()
self.cf.read(self.path)
def get(self, field, key):
result = ""
try:
result = self.cf.get(field, key)
except:
result = ""
return result
def set(self, field, key, value):
try:
self.cf.set(field, key, value)
self.cf.write(open(self.path,'w'))
except:
return False
return True
if __name__ == "__main__":
config_file_path = 'config.ini'
print read_config(config_file_path, 'wei', "web")
write_config(config_file_path, 'wei', "web2", '192.168.0.101')
print read_config(config_file_path, 'wei', "web2")