Python3.5 sample method for modifying iis web. CONFIG by programming,

Source: Internet
Author: User

Python3.5 sample method for modifying iis web. CONFIG by programming,

This article describes how to modify iis web. CONFIG by using Python3.5 programming. We will share this with you for your reference. The details are as follows:

#! /Usr/bin/env python3.5 #-*-coding: utf8-*-from xml. etree. elementTree import ElementTree, Elementdef read_xml (in_path): "read and parse the XML file: param in_path: XML Path: return:" tree = ElementTree () tree. parse (in_path) return treedef write_xml (tree, out_path): "" write the XML file: param tree: param out_path: Write path: return: "" tree. write (out_path, encoding = "UTF-8", xml_declaration = True) def if_match (node, kv_map): "Judgment Whether a node contains all input parameter attributes: param node: param kv_map: MAP: return: "for key in kv_map: if node. get (key )! = Kv_map.get (key): return False return Truedef find_nodes (tree, path): "" Find all nodes matching a path: param tree: XML tree: param path: Node path: return: "" return tree. findall (path) def get_node_by_keyvalue (nodelist, kv_map): "" locate the matched Node Based on the attribute and attribute value. Return node: param nodelist: node list: param kv_map: matching attributes and attribute values MAP: return: "" result_nodes = [] for node in nodelist: if if_match (node, kv_map): result_nodes.append (node) return result_nodesd Ef change_node_properties (nodelist, kv_map, is_delete = False): "Modify, add, and delete node attributes and attribute values: param nodelist: node list: param kv_map: attribute and attribute value 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): "changes, adds, and deletes the text of a node: param nodelist: Section Vertex list: param text: updated 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 = textdef create_node (tag, property_map, content): "Create a new node: param tag: node tag: param property_map: attribute and attribute value MAP: param content: file Content in the closed label of a node: return: new node "element = Element (tag, property_map) element. text = content return elementdef add_ch Ild_node (nodelist, element): "add a subnode to a node: param nodelist: node list: param element: subnode: return:" for node in nodelist: node. append (element) def del_node_by_tagkeyvalue (nodelist, tag, kv_map): "locates a node with the same attribute and attribute value, and deletes it: param nodelist: parent node list: param tag: child node label: param kv_map: attribute and Attribute Value List: 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): "fixes XML configuration files to meet IIS: param file: target 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 ("configuration file % s, modification successful! "% File) if _ name _ =" _ main _ ": #1. read xml file tree = read_xml ("web. config ") # Find the parent node print () nodes = find_nodes (tree," connectionStrings/") # accurately locate the child node result_nodes = (get_node_by_keyvalue (nodes, {" name ": "strConnection_HuaChenShiYou"}) # modify the node attributes change_node_properties (result_nodes, {"connectionString": "UwreW/w% 2BMAvpzqlEZP2JZqrVEofP3AulFUZbTLfmndYFRqIytlxSCeHr2A79EWHH9% 2BMAvpzqlEZP2JZqrVEofP3AulFUZbTLfmndYFRqIytlxSCeHr2A79EWHH9/xg0eSgsdvWd"}) #2. attribute Modification #. find the parent node # nodes = find_nodes (tree, "processers/processer") # B. accurately locate the subnode # result_nodes = get_node_by_keyvalue (nodes, {"name": "BProcesser"}) # C. modify node attributes # change_node_properties (result_nodes, {"age": "1"}) # D. delete node attributes # change_node_properties (result_nodes, {"value": ""}, True) ## 3. node modification #. create a node # a = create_node ("person", {"age": "15", "money": "200000"}, "this is the firest content ") # B. insert to the parent node # add_child_node (result_nodes, a) ### 4. delete node # locate parent node # del_parent_nodes = find_nodes (tree, "processers/services/service") # locate and delete the child node accurately # target_del_node = del_node_by_tagkeyvalue (del_parent_nodes, "chain", {"sequency": "chain1"}) ### 5. modify node text # locate node # text_nodes = get_node_by_keyvalue (find_nodes (tree, "processers/services/service/chain"), {"sequency": "chain3 "}) # change_node_text (text_nodes, "new text") ### 6. output to the result file write_xml (tree, "new. config ") config_file_rw (" new. config ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.