1 Star 0 Fork 1

PASSLINK/基于PyQt5_Socket通信的监控系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ConfigEdit.py 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
PASSLINK 提交于 1年前 . Config参数修改配置
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xcbnh/py-qt5-socket.git
git@gitee.com:xcbnh/py-qt5-socket.git
xcbnh
py-qt5-socket
基于PyQt5_Socket通信的监控系统
master

搜索帮助