将做工程过程中重要的一些代码段收藏起来,下面代码段是关于python检测RabbitMQ的状态是否正常的代码。

import socketdef 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 Falsecheck_aliveness('127.0.0.1', 15672)