【原文】https://www.cnblogs.com/alfred0311/p/7944199.html
应用场景描述:
通过配置文件获取服务器上配置的服务名及运行端口号,编写python脚本检测服务上服务是否在运行?
#!/usr/bin/env python#-*- coding:utf-8 -*-#fileName: config.py#服务配置
classconfig:
serviceList= 'service1,service2,service3'service1= '服务1'service1Port= 8001service2= '服务2'service2Port= 8002service3= '服务3'service3Port= 8003
#!/usr/bin/env python#-*- coding:utf-8 -*-#fileName: envCheck.py
importsocketfrom config importconfig
config=config
serviceList=config.serviceList#判断某端口服务是否运行
defportCheck(host, port):
sk=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1)try:
sk.connect((host, port))#print '在服务器 %s 上服务端口 %d 的服务正在运行!' % (host, port)
returnTrueexceptException:#print '在服务器 %s 上服务端口 %d 的服务未运行!' % (h