Python3.5编程实现修改IIS WEB.CONFIG的方法示例
作者:水·域 时间:2022-04-22 15:27:55
本文实例讲述了Python3.5编程实现修改IIS WEB.CONFIG的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/env python3.5
# -*- coding:utf8 -*-
from xml.etree.ElementTree import ElementTree,Element
def read_xml(in_path):
"""
读取并解析XML文件
:param in_path: XML路径
:return:
"""
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree,out_path):
"""
将XML文件写出
:param tree:
:param out_path:写出路径
:return:
"""
tree.write(out_path,encoding="utf-8",xml_declaration=True)
def if_match(node,kv_map):
"""
判断某个节点是否包含所有传入参数属性
:param node: 节点
:param kv_map: 属性及属性值组成的MAP
:return:
"""
for key in kv_map:
if node.get(key) != kv_map.get(key):
return False
return True
def find_nodes(tree,path):
"""
查找某个路径匹配的所有节点
:param tree:XML树
:param path:节点路径
:return:
"""
return tree.findall(path)
def get_node_by_keyvalue(nodelist,kv_map):
"""
根据属性及属性值定位符合的节点,返回节点
:param nodelist: 节点列表
:param kv_map: 匹配属性及属性值MAP
:return:
"""
result_nodes =[]
for node in nodelist:
if if_match(node,kv_map):
result_nodes.append(node)
return result_nodes
def change_node_properties(nodelist,kv_map,is_delete =False):
"""
修改、增加、删除 节点的属性及属性值
:param nodelist: 节点列表
:param kv_map: 属性及属性值MAP
:param is_delete:
:return:
"""
for node in nodelist:
for key in kv_map:
if is_delete:
if key in node.attrib:
del node.attrib[key]
else:
node.set(key,kv_map.get(key))
def change_node_text(nodelist,text,is_add=False,is_delete=False):
"""
改变、增加、删除一个节点的文本
:param nodelist: 节点列表
:param text: 更新后的文本
:param is_add:
:param is_delete:
:return:
"""
for node in nodelist:
if is_add:
node.text += text
elif is_delete:
node.text = ""
else:
node.text = text
def create_node(tag,property_map,content):
"""
新造一个节点
:param tag:节点标签
:param property_map:属性及属性值MAP
:param content: 节点闭合标签里的文件内容
:return:新节点
"""
element =Element(tag,property_map)
element.text =content
return element
def add_child_node(nodelist,element):
"""
给一个节点添加子节点
:param nodelist: 节点列表
:param element: 子节点
:return:
"""
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist,tag,kv_map):
"""
同过属性及属性值定位一个节点,并删除之
:param nodelist: 父节点列表
:param tag: 子节点标签
:param kv_map: 属性及属性值列表
:return:
"""
for parent_node in nodelist:
childree = parent_node.getchildren()
for child in childree:
if child.tag == tag and if_match(child,kv_map):
parent_node.remove(child)
def config_file_rw(file):
"""
对XML配置文件进行修复以满足适应IIS
:param file: 目标文件
:return:
"""
import re
x =re.compile("<ns0:")
y = re.compile("</ns0:")
z = re.compile("xmlns:ns0")
with open(file,"r",encoding="utf-8") as f:
txt = f.readlines()
for i, line in enumerate(txt):
if x.search(line):
txt[i] = x.sub("<", line)
elif y.search(line):
txt[i] = y.sub("</", line)
elif z.search(line):
txt[i] = "<configuration>\n"
with open(file,"w",encoding="utf-8") as fw:
fw.writelines(txt)
fw.close()
print("配置文件%s,修改成功!"%file)
if __name__ == "__main__":
#1. 读取xml文件
tree = read_xml("web.config")
# 找到父节点
print()
nodes = find_nodes(tree, "connectionStrings/")
# 通过属性准确定位子节点
result_nodes =(get_node_by_keyvalue(nodes,{"name":"strConnection_HuaChenShiYou"}))
# 修改节点属性
change_node_properties(result_nodes,{"connectionString":"UwreW/Obe4fGk2CFW4uE6iZWaPAVn0nePXIrtNRApxEGLv6SHetFg6b%2BMLFhg9myAr2EI2b3FgHtKHOKVcjz5DPoV8%2BMAvpzqlEZP2JZqrVEofP3AulFUZbTLfmndYFRqIytlxSCeHr2A79EWHH9/xg0eSgsdvWd"})
# #2. 属性修改
# #A. 找到父节点
# nodes = find_nodes(tree, "processers/processer")
# #B. 通过属性准确定位子节点
# result_nodes = get_node_by_keyvalue(nodes, {"name":"BProcesser"})
# #C. 修改节点属性
# change_node_properties(result_nodes, {"age": "1"})
# #D. 删除节点属性
# change_node_properties(result_nodes, {"value":""}, True)
#
# #3. 节点修改
# #A.新建节点
# a = create_node("person", {"age":"15","money":"200000"}, "this is the firest content")
# #B.插入到父节点之下
# add_child_node(result_nodes, a)
#
# #4. 删除节点
# #定位父节点
# del_parent_nodes = find_nodes(tree, "processers/services/service")
# #准确定位子节点并删除之
# target_del_node = del_node_by_tagkeyvalue(del_parent_nodes, "chain", {"sequency" : "chain1"})
#
# #5. 修改节点文本
# #定位节点
# text_nodes = get_node_by_keyvalue(find_nodes(tree, "processers/services/service/chain"), {"sequency":"chain3"})
# change_node_text(text_nodes, "new text")
#
# #6. 输出到结果文件
write_xml(tree, "new.config")
config_file_rw("new.config")
希望本文所述对大家Python程序设计有所帮助。
来源:http://www.cnblogs.com/IPYQ/p/6281440.html
标签:Python,IIS,WEB.CONFIG
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用webbrowser浏览指定url的方法
2023-10-24 03:33:31
立足于传统行业设计
2008-07-26 12:36:00
200 行python 代码实现 2048 游戏
2021-08-06 16:17:35
![](https://img.aspxhome.com/file/2023/9/63879_0s.jpg)
Python的Web框架Django介绍与安装方法
2021-03-06 05:46:12
![](https://img.aspxhome.com/file/2023/5/93855_0s.png)
python爬虫基础之urllib的使用
2022-02-10 19:01:18
Python中的TCP socket写法示例
2023-06-25 00:21:05
![](https://img.aspxhome.com/file/2023/7/60707_0s.jpg)
Python基于Tensorflow2.X实现汽车油耗预测
2021-05-05 05:25:22
![](https://img.aspxhome.com/file/2023/3/72743_0s.png)
python生成指定尺寸缩略图的示例
2022-04-14 01:14:49
PHP输出JSON格式数据方式
2023-06-24 06:39:09
![](https://img.aspxhome.com/file/2023/9/55519_0s.png)
Python callable内置函数原理解析
2022-02-16 11:27:03
Python 任务自动化工具nox 的配置与 API详情
2022-05-02 20:57:30
PyQt5每天必学之像素图控件QPixmap
2022-04-05 04:42:54
![](https://img.aspxhome.com/file/2023/4/105454_0s.jpg)
python实现滑雪游戏
2021-10-08 05:20:35
python使用urllib2模块获取gravatar头像实例
2022-12-11 03:16:36
python写入并获取剪切板内容的实例
2023-08-03 10:44:04
linux中使用boost.python调用c++动态库的方法
2023-01-19 19:21:03
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2021-06-16 19:53:55
![](https://img.aspxhome.com/file/2023/5/107575_0s.png)
python列表添加元素append(),extend(),insert(),+list的区别及说明
2022-12-11 08:48:35
![](https://img.aspxhome.com/file/2023/1/95161_0s.png)
以用户为中心的WEB表单
2010-08-03 12:11:00
![](https://img.aspxhome.com/file/UploadPic/20108/3/01-37s.jpg)
分享Python 中的 7 种交叉验证方法
2023-09-18 19:10:33
![](https://img.aspxhome.com/file/2023/1/81481_0s.png)