/
read_xml_ip_port.py
58 lines (50 loc) · 2.19 KB
/
read_xml_ip_port.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
from xml.dom.minidom import parse
import xml.etree.ElementTree as ET
import socket
add_index = 0
def cat_xml(bin_dir, res_dir):
local_ip = socket.gethostbyname(socket.gethostname())
for root, dirs, files in os.walk(rootdir):
if 'server' not in root:
continue
#print('now dir', root)
for file in files:
if 'config.xml' not in file:
continue
xml_path = os.path.join(root, file)
print('xml path', root, file)
#xmldoc = ET.ElementTree(xml_path)
xmldoc = parse(xml_path)
inet_c_list = xmldoc.getElementsByTagName('inet_c')
if len(inet_c_list) > 0:
c_ip = inet_c_list[0].attributes['IP'].value
c_port = inet_c_list[0].attributes['Port'].value
inet_c_list[0].attributes['Port'].value = str(int(c_port) + add_index)
inet_c_list[0].attributes['IP'].value = local_ip
inet_s_list = xmldoc.getElementsByTagName('inet_s')
if len(inet_s_list) > 0:
s_ip = inet_s_list[0].attributes['IP'].value
s_port = inet_s_list[0].attributes['Port'].value
inet_s_list[0].attributes['Port'].value = str(int(s_port) + add_index)
inet_s_list[0].attributes['IP'].value = local_ip
if 'loginserver' in root:
enet_list = xmldoc.getElementsByTagName('enet')
if len(enet_list) > 0:
enet_list[0].attributes['IP'].value = local_ip
print('set login eip', local_ip)
new_str = xmldoc.toxml()
#xmldoc.close()
#print(new_str)
#new_file_name = file
sub_dir = root[root.rfind('\\')+1:len(root)]
print('sub_dir', sub_dir)
new_file_path = os.path.join(res_dir, sub_dir)
new_file_path = os.path.join(new_file_path, file)
print('new_file_path', new_file_path)
myfile = open(new_file_path, "w")
myfile.write(new_str)
myfile.close()
rootdir = r'D:\test_config\server_config'
res_dir = r'D:\test_config\server_config_1'
cat_xml(rootdir, res_dir)