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+OpenCV实战之实现文档扫描 2022-10-20
- python验证多组数据之间有无显著差异 2023-08-08
- MySQL数据优化-多层索引 2023-08-11
- linux 安装 python3 2023-09-03
- windows下面使用多版本Python安装指定版本的虚拟环境 2023-09-04
- 云服务器Ubuntu更改默认python版本 2023-09-03
- 远程和Ubuntu服务器进行Socket通信,使用python和C#(准备篇) 2023-09-05
- 创建python虚拟环境(在ubuntu16.04中) 2023-09-04
- Python爬虫爬取属于自己的地铁线路图 2023-08-05
- 使用Pycharm创建一个Django项目的超详细图文教程 2022-09-02
