將做工程過程中重要的一些代碼段收藏起來,下面代碼段是關(guān)于python檢測RabbitMQ的狀態(tài)是否正常的代碼。

            
              import socket

def check_aliveness(ip, port):
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.settimeout(1)
    try:
        sk.connect((ip,port))
        print 'service is OK!'
        return True
    except Exception:
        print 'service is NOT OK!'
        return False
    finally:
        sk.close()
    return False

check_aliveness('127.0.0.1', 15672)