Python 处理config.ini文件
来源:互联网 发布:打印发票软件 编辑:程序博客网 时间:2024/05/17 22:11
在做一个自动化测试框架中需要根据不同设备修改IP、username、password等信息,开始使用excel,单使用下来发现excel较死板,不方便后续维护。且功能自动化只针对单台设备,后续需要在UI上实现。确定使用config文件,放在程序中,使用时UI直接修改即可。
先了解ini文件的格式
[common] -----section
ip_addr = 192.168.25.71 ------option=value
username = admin
password = admin123
ssh_port = 22
telnet_port = 17230
读取和设置的代码如下
class Getipcmessage():
def __init__(self): ##获取配置文件路径及打开获取ini文件sections
self.dirpwd = os.path.abspath('..')
self.cfgname = r'%s\Config\IPCconfig.ini' %self.dirpwd
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.cfgname)
secs = self.cf.sections()
opts = self.cf.options("common")
def getcfgmessage(self): ##获取配置文件中信息
self.ip = self.cf.get("common","ip_addr")
self.username = self.cf.get("common","username")
self.password = self.cf.get("common","password")
self.SSH_port = self.cf.get("common","SSH_port")
self.telnet_port = self.cf.get("common","telnet_port")
print self.ip,self.username,self.password,self.SSH_port
def setcfgip(self,value): ##设置IP信息到配置文件
self.cf.set("common", "ip_addr", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgusername(self,value): ##设置username信息到配置文件
self.cf.set("common", "username", value)
self.cf.write(open(self.cfgname,"w"))
def setcfguspassword(self,value): ##设置password信息到配置文件
self.cf.set("common", "password", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgusshport(self,value): ##设置ssh_port信息到配置文件
self.cf.set("common", "ssh_port", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgutelnetport(self,value): ##设置telnet_port信息到配置文件
self.cf.set("common", "telnet_port", value)
self.cf.write(open(self.cfgname,"w"))
def __init__(self): ##获取配置文件路径及打开获取ini文件sections
self.dirpwd = os.path.abspath('..')
self.cfgname = r'%s\Config\IPCconfig.ini' %self.dirpwd
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.cfgname)
secs = self.cf.sections()
opts = self.cf.options("common")
def getcfgmessage(self): ##获取配置文件中信息
self.ip = self.cf.get("common","ip_addr")
self.username = self.cf.get("common","username")
self.password = self.cf.get("common","password")
self.SSH_port = self.cf.get("common","SSH_port")
self.telnet_port = self.cf.get("common","telnet_port")
print self.ip,self.username,self.password,self.SSH_port
def setcfgip(self,value): ##设置IP信息到配置文件
self.cf.set("common", "ip_addr", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgusername(self,value): ##设置username信息到配置文件
self.cf.set("common", "username", value)
self.cf.write(open(self.cfgname,"w"))
def setcfguspassword(self,value): ##设置password信息到配置文件
self.cf.set("common", "password", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgusshport(self,value): ##设置ssh_port信息到配置文件
self.cf.set("common", "ssh_port", value)
self.cf.write(open(self.cfgname,"w"))
def setcfgutelnetport(self,value): ##设置telnet_port信息到配置文件
self.cf.set("common", "telnet_port", value)
self.cf.write(open(self.cfgname,"w"))
1 0
- Python 处理config.ini文件
- 4、Python 处理 ini 文件
- Python 配置处理类(ini文件)
- 读取Config.ini文件内容
- vfp读取config.ini文件
- C# 读写 config.ini 文件
- Python 处理 ini 格式文件
- python ini配置文件处理
- [Python] 文件读写——处理ini配置文件
- 处理INI文件
- c# INI文件处理
- INI 文件的处理
- python Ini文件读写
- python讀寫ini文件
- python 解析ini 文件
- Python读写ini文件
- Python ini文件操作
- Python 操作ini文件
- 39.Which two statements are true about standard database auditing? (Choose two.)
- C# ListView用法详解
- Git-Git基础
- Android基础-- 利用handler发送消息报:"This message is already in use"解决办法
- TG(提高篇)第15讲 实践项目 简单银行系统(函数篇)
- Python 处理config.ini文件
- [2016/7/20][usaco 2.1]Hamming Codes
- Codeforces 698C. Fix a Tree (并查集)
- 图片的高斯模糊
- [树] cf 698B D Fix a Tree
- opencv鼠标响应事件
- 混日子不是你的错,根源在这里
- 五大常用算法之五:分支限界法
- NGINX 结合 lua 动态修改upstream