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
猜你喜欢
- 在centos6.4下安装python3.5 2023-09-04
- Python实现将DNA序列存储为tfr文件并读取流程介绍 2022-10-20
- windows安装python2.7.12和pycharm2018教程 2023-09-03
- CentOS7 安装 Python3.6 2023-09-04
- Python之路-Python中的线程与进程 2023-09-04
- python中defaultdict用法实例详解 2022-10-20
- Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc) 2023-08-04
- Python 保存数据的方法(4种方法) 2023-09-04
- python中列表添加元素的几种方式(+、append()、ext 2022-09-02
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor 2023-09-04