用Python在Tomcat成功启动后自动打开浏览器访问Web应用
前提条件
- Windows
- Python 2.7
- 需设置
CATALINA_HOME
环境变量
放码过来
# -*- coding: utf-8 -*import os
import time
import subprocess
tomcatStartFilePath = "C:\tomcat\apache-tomcat-7.0.90-windows-x64\apache-tomcat-7.0.90\bin\startup.bat"
browserPath = "C:\Users\Administrator.USER-20180302VA\AppData\Local\360Chrome\Chrome\Application\360chrome.exe"
appAddress = "http://localhost:8080/nice"
# 启动 tomcat,注意要设置CATALINA_HOME的环境变量
subprocess.Popen(tomcatStartFilePath, shell=True)
print "Starting tomcat..."
time.sleep(15)
# 启动15s后, 轮询 8080端口是否启用
print "Polling..."
startBrowerFalg = False
#每次轮询间隔5秒
interval = 5
count = 6
while count > 0:
tmpFile = os.popen("netstat -na","r")
breakWhileFlag = False
for line in tmpFile.readlines():
if line.startswith(" TCP 0.0.0.0:8080"):
breakWhileFlag = True
break
print "Not yet."
if breakWhileFlag:
print "It"s Ok."
startBrowerFalg = True
break
else:
count -= 1
time.sleep(interval)
# 8080 启用成功后, 打开浏览器访问 /nice
if startBrowerFalg:
print "Launch the browser."
subprocess.Popen("%s %s"%(browserPath, appAddress))
else:
print "Something wrong ..."
raw_input()
参考资料
- 如何通过批处理调用tomcat的startup.bat,然后在tomcat启动成功后,自动打开浏览器并定位到指定网址
- Python编程快速上手
以上是 用Python在Tomcat成功启动后自动打开浏览器访问Web应用 的全部内容, 来源链接: utcz.com/z/514569.html