def killPid(port,num):
import os, time
for i in range(num):
time.sleep(1)
result = os.popen('netstat -ano|findstr "%s"' %port)
re = result.readlines()
if "LISTENING" not in str(re):
print("端口号:%s已清空所有占用" % port)
break
else:
for i in re:
if "LISTENING" in i:
pid = str(i).split("LISTENING")[1].strip()
print(pid)
os.popen("taskkill /pid %s /f" %pid)
killPid("5037",3)
10968
端口号:5037已清空所有占用