import socketimport osimport os.pathimport shutilimport time# 获取本机IP地址def get_host_ip():get host ip address获取本机IP地址:return:s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)t...
import socket import os import os.path import shutil import time # 获取本机IP地址 def get_host_ip(): """ get host ip address 获取本机IP地址 :return: """ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() return ip #检测端口是否被占用 def is_port_used(ip, port): """ check whether the port is used by other program 检测端口是否被占用 :param ip: :param port: :return: """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((ip, port)) return True except OSError: return False finally: s.close() # 启动tomcat def startTomcat(): os.chdir(r"E:\\aaaaa\\apache-tomcat\\apache-tomcat-8.0.11\\bin") os.system(".\startup.bat") # 关闭tomcat def shutdownTomcat(): os.chdir(r"E:\\aaaaa\\apache-tomcat\\apache-tomcat-8.0.11\\bin") os.system(".\shutdown.bat") CUR_PATH = r'E:\aaaaa\apache-tomcat\apache-tomcat-8.0.11\work\Catalina' #删除当前目录下的全部 def del_file(path): shutil.rmtree(CUR_PATH) path=r'E:\aaaaa\apache-tomcat\apache-tomcat-8.0.11\webapps' #当前目录中需要保留的文件 #filesList=['a.txt','b.txt'] #当前目录中需要保留的文件夹 dirsList=['docs','examples','host-manager','manager','ROOT'] #获取文件后缀名 def suffix( file, *suffixName ) : array = map( file.endswith, suffixName ) if True in array : return True else : return False def DeleteFiles(path, remainDirsList): dirsList = os.listdir(path) for f in dirsList: if f not in remainDirsList: filePath = os.path.join(path,f) if os.path.isdir(filePath): shutil.rmtree(filePath, True) if suffix( f,'.war'): os.remove(filePath) # 测试 if __name__ == '__main__': host_ip = get_host_ip() print(host_ip) flag=is_port_used(host_ip, 8080) print(flag) if flag: print('关闭----》删除-----》开启') print('开始关闭') shutdownTomcat() time.sleep(5) print('开始删除') del_file(CUR_PATH) DeleteFiles(path,dirsList) time.sleep(10) print('开始重启') startTomcat() else: print('删除-----》开启') del_file(CUR_PATH) DeleteFiles(path,dirsList) print('关闭----》开启2') startTomcat()
沃梦达教程
本文标题为:windows python2下停止,清空,启动tomcat
基础教程推荐
猜你喜欢
- 基于Python实现股票数据分析的可视化 2023-08-04
- Python基础学习之函数和代码复用详解 2022-09-02
- CentOS 7.5 安装 Python3.7 2023-09-03
- ubuntu 18 python3.6 的安装与 python2的版本切换 2023-09-03
- Python 中 Elias Delta 编码详情 2023-08-08
- 四步教你学会打包一个新的Python模块 2022-10-20
- Python爬取当网书籍数据并数据可视化展示 2023-08-11
- python的环境conda简介 2022-10-20
- centos系统 anaconda3(python3)安装pygrib 2023-09-04
- Centos7下安装python环境 2023-09-04