代码拉取完成,页面将自动刷新
import configparser
import os
class ConfigClass():
"""
配置文件
"""
def __init__(self):
self.Server_Addr = "127.0.0.1"
self.Server_Port = 8800
self.Server_DataSize = 1024
self.Server_MaxClient = 5
self.Client_Addr = "127.0.0.1"
self.Client_Port = 8000
self.Client_DataSize = 1024
self.MySql_username = "root"
self.MySql_password = "root"
self.MySql_database = "my_data"
self.MySql_Port = 3306
self.file_path = 'Config.conf'
if not os.path.exists(self.file_path):
print("文件不存在")
return None
config = configparser.ConfigParser()
config.read(self.file_path)
if 'Server' in config:
self.Server_Addr = config.get('Server', 'Addr', fallback=self.Server_Addr)
self.Server_Port = int(config.getint('Server', 'Port', fallback=self.Server_Port))
self.Server_DataSize = int(config.getint('Server', 'DataSize', fallback=self.Server_DataSize))
self.Server_MaxClient = config.getint('Server', 'MaxClient', fallback=self.Server_MaxClient)
if 'Client' in config:
self.Client_Addr = config.get('Client', 'Addr', fallback=self.Client_Addr)
self.Client_Port = int(config.getint('Client', 'Port', fallback=self.Client_Port))
self.Client_DataSize = int(config.getint('Client', 'DataSize', fallback=self.Client_DataSize))
def DefalutConfig(self):
"""
恢复默认配置
:return:
"""
if not os.path.exists(self.file_path):
print("文件不存在")
return None
config = configparser.ConfigParser()
config.read(self.file_path)
config.set("Server", "Addr", "127.0.0.1")
config.set("Server", "Port", "8800")
config.set("Server", "DataSize", "1024")
config.set("Server", "MaxClient", "5")
config.set("Client", "Addr", "127.0.0.1")
config.set("Client", "Port", "8000")
config.set("Client", "DataSize", "1024")
config.set("MySql", "username", "root")
config.set("MySql", "password", "root")
config.set("MySql", "database", "my_data")
config.set("MySql", "Port", "3306")
# 写入更新后的配置到文件中
with open(self.file_path, 'w') as configfile:
config.write(configfile)
def EditConfig(self, Section, Option, Value):
"""
修改配置
:param Section:
:param Option:
:param Value:
:return:
"""
if not os.path.exists(self.file_path):
print("文件不存在")
return None
config = configparser.ConfigParser()
try:
config.read(self.file_path)
config.set(str(Section), str(Option), str(Value))
except Exception as e:
config.add_section(str(Section))
config.set(str(Section), str(Option), str(Value))
finally:
# 写入更新后的配置到文件中
with open(self.file_path, 'w') as configfile:
config.write(configfile)
Config = ConfigClass()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。