Python替换多个tomcat端口号
2019独角兽企业重金招聘Python工程师标准>>>
批量替换tomcat端口号:
tomcat_port.py:
# -*- coding: utf-8 -*-
import osfrom famiover.util.os_util import is_windowsdef get_tomcats_config():"""获取tomcat端口修改配置"""configs = []tomcat_info_8081 = {"is_delete_comments": True,"path": "/Users/famiover/Downloads/apache-tomcat-8081","p8005": "8006","p8080": "8081","p8009": "8010",}tomcat_info_8082 = {"is_delete_comments": False,"path": "/Users/famiover/Downloads/apache-tomcat-8082","p8005": "8007","p8080": "8082","p8009": "8011",}configs.append(tomcat_info_8081)configs.append(tomcat_info_8082)return configsdef get_file_path(file_path):if not is_windows():if file_path.endswith("/"):file_path += "conf/server.xml"else:file_path += "/conf/server.xml"else:if file_path.endswith("\\"):file_path += "conf\\server.xml"else:file_path += "\\conf\\server.xml"return file_pathdef delete_comments(config):"""去掉注释、空行"""file_path = get_file_path(config.get("path"))if not os.path.exists(file_path):print(file_path, "不存在!")exit(0)with open(file_path, 'r') as r:lines = r.readlines()r.close()is_enable_write = Falsewith open(file_path, 'w') as w:for index, line in enumerate(lines):strip_line = line.strip()if len(strip_line) == 0:continueswitch1 = strip_line[0:4] == "<!--" and strip_line.endswith("-->")if switch1:continueswitch2 = strip_line[0:4] == "<!--" and not strip_line.endswith("-->")if switch2:is_enable_write = Falsecontinueelse:if strip_line.endswith("-->"):is_enable_write = Truecontinueelse:if is_enable_write or index == 0:w.write(line)else:continuew.close()def validate_port():"""验证是否有重复端口号"""configs = get_tomcats_config()config_ports = ["8005", "8080", "8009"]for config in configs:config_ports.append(config.get("p8005"))config_ports.append(config.get("p8080"))config_ports.append(config.get("p8009"))for config_port in config_ports:if config_ports.count(config_port) > 1:print("端口配置有重复的,请更正!")exit(0)def replace_port(config):file_path = config.get("path")if file_path.endswith("/"):file_path += "conf/server.xml"else:file_path += "/conf/server.xml"if not os.path.exists(file_path):print(file_path, "不存在!")exit(0)with open(file_path, 'r') as r:lines = r.readlines()r.close()with open(file_path, 'w') as w:for line in lines:if "8005" in line:line = line.replace("8005", config.get("p8005"))if "8080" in line:line = line.replace("8080", config.get("p8080"))if "8009" in line:line = line.replace("8009", config.get("p8009"))w.write(line)w.close()def replace():validate_port()configs = get_tomcats_config()for config in configs:if config.get("is_delete_comments",False):delete_comments(config)replace_port(config)if __name__ == '__main__':replace()
转载于:https://my.oschina.net/famiover/blog/776056
Python替换多个tomcat端口号相关推荐
- java关闭tomcat服务器端口_关闭tomcat端口号
一. CentOS 关闭tomcat端口号 1. 首先保证liunx下 ps -ef | grep java 2. 会显示如下信息 我使用的是IDEA打包的war包.tomcat是自带的 3. 查看未 ...
- 修改Tomcat端口号
8080是Tomcat服务器的默认端口号.我们可以通过修改Tomcat/conf文件夹下的主配置文件server.xml来更改端口号.用记事本打开server.xml文件,找出出现以下代码的部分: & ...
- CentOS修改Tomcat端口号
Linux下修改Tomcat默认端口 1.方法一 假设tomcat所在目录为/usr/local/apache-tomcat/ 1.打开tomcat配置文件 # vi /usr/local/apach ...
- Docker中修改Tomcat端口号
docker run -p 80:8080 tomcat 将宿主机的80端口映射到Docker Tomcat服务器的8080端口 docker container ls -a = docker ps ...
- tomcat默认端口号(三个tomcat端口号)
tomcat默认端口号(三个tomcat端口号) 2020-05-08 10:43:21 共10个回答 Tomcat的默认端口号是多少 您好,提问者:Tomcat的默认端口号是:8080.weblog ...
- linux修改tomcat端口号为80不起作用
1.修改tomcat端口号 第一步:进入tomcat安装目录/conf 第二步:编辑server.xml文件 修改端口号8080为80 保存退出 第三步:关闭防火墙 此时用80端口号在浏览器不能打开 ...
- eclipse查看、修改tomcat端口号
方法一: 1.eclipse里双击server下的本地服务器 2.窗口中找到tomcat配置ports 3.可以点击对应的端口号,修改保存. 方法二:找到eclipse的工作空间workspace,找 ...
- Linux 修改tomcat端口号
Tomcat服务器的默认的端口号为8080,可以通过修改Tomcat服务器的conf目录下的主配置文件server.xml来更改. <Connector port="8080" ...
- 如何修改Tomcat端口号
1.首先需要了解Tomcat默认的端口号是:8080 2.点击进入 Tomcat 目录下的 conf 目录,找到 server.xml 配置文件并打开 3.找到Connector标签,修改port属性 ...
- python flask 如何修改默认端口号
场景:按照github文档上启动一个flask的app,默认是用5000端口,如果5000端口被占用,启动失败. 样例代码: from flask import Flaskapp = Flask(__ ...
最新文章
- 【NLP-NER】命名实体识别中最常用的两种深度学习模型
- 00075_BigInteger
- 麻省理工学院(MIT)研究生学习指导—— 怎样做研究生
- c语言高低位拷贝_C语言中的大小端转换与高低位颠倒
- 计算机二级考试考不考二进制,如何通过计算机一、二级考试?方法很重要,过来人的经验告诉你...
- Ubuntu上安装OpenGL
- mysql临键锁_详解 MySql InnoDB 中的三种行锁(记录锁、间隙锁与临键锁)
- 使用 WordPress 自定义字段功能为文章添加下载按钮
- 搭积木php编程,Primo Toys,一款可以同时搭积木与编程的玩具
- CE教程:植物大战僵尸(太阳数值修改)
- Python —— 数据科学的手段 (第2版)配套代码
- 两年多工作心得和体会
- mac版Mysql可视化工具 - Sequel Pro
- CSDN为什么会发展社区?看看官方怎么说
- BCD码和十进制的转换
- 提起薛定谔,除了“猫”你还能想到啥?
- Android判断当前是不是黑色主题
- 2022年危险化学品生产单位安全生产管理人员特种作业证考试题库模拟考试平台操作
- C语言每日一练——第57天:递归解决分鱼问题
- 磁盘分区 (MBR, GPT)、启动流程 (BIOS, UEFI)