python实现系统状态监测和故障转移实例方法

1153次阅读  |  发布于5年以前

复制代码 代码如下:

coding: utf-8

import socket
import select
import time
import os
import threading

def ser():
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(("",43244))
while 1:
infds,outfds,errfds = select.select([s],[],[],5)
if infds:
sms = s.recv(1024)
if sms=="alived":
print "peer is alived"
else:
print "Can't hear peer!"
os.system("./failover.sh")

def clt():
while 1:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('192.168.10.1', 43244))
sock.send("alived")
time.sleep(2)

if name=="main":
ser=threading.Thread(target=ser)
clt=threading.Thread(target=clt)
ser.start()
clt.start()
ser.join()
clt.join()

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8