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

yipeiwu_com6年前Python基础

复制代码 代码如下:

#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()

failover.sh

复制代码 代码如下:

#!/bin/bash

vip=8.8.8.8

vip_local=`ifconfig |grep -A 1 "eth0:0" |awk '/inet addr/{print $2}'|cut -d ":" -f2`

if [ ! $vip_local ];then ifconfig eth0:0 $vip netmask 255.255.255.0 up;fi

相关文章

用python实现英文字母和相应序数转换的方法

第一步:字母转数字 英文字母转对应数字相对简单,可以在命令行输入一行需要转换的英文字母,然后对每一个字母在整个字母表中匹配,并返回相应的位数,然后累加这些位数即可。过程中,为了使结果更...

新手入门Python编程的8个实用建议

新手入门Python编程的8个实用建议

前言 我们在用Python进行机器学习建模项目的时候,每个人都会有自己的一套项目文件管理的习惯,我自己也有一套方法,是自己曾经踩过的坑踩过的雷总结出来的,现在在这里分享一下给大家,因为很...

python的移位操作实现详解

因为要将js的一个签名算法移植到python上,遇到一些麻烦。 int无限宽度,不会溢出 算法中需要用到了32位int的溢出来参与运算,但是python的int是不会溢出的,达到界限后...

答题辅助python代码实现

本文实例为大家分享了答题辅助python具体代码,供大家参考,具体内容如下 from screenshot import pull_screenshot import time, u...

python3如何将docx转换成pdf文件

本文实例为大家分享了python3将docx转换成pdf文件的具体代码,供大家参考,具体内容如下 直接上代码 # -*- encoding:utf-8 -*- """ auth...