#!/usr/bin/python #$Id: zeo_ping.py 2338 2005-07-02 00:39:31Z sidnei $ # Licensed under the Enfold Enterprise Server License Agreement # Copyright(c), 2004-5, Enfold Systems, LLC, ALL RIGHTS RESERVED import re import socket SERVER_PROTOCOL = re.compile('Z\d{3}$') def zeo_ping(host, port, timeout=5, reraise=False, handshake=False): """Ping a supposed ZEO server and see if it's up and running. host hostname or ip to connect to port port to use for connection timeout maximum time in seconds to wait for a connection reraise if a connection error occurs, re-raise the exception or not handshake try a zeo handshake """ port = int(port) timeout = float(timeout) reraise = bool(reraise) handshake = bool(handshake) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(timeout) try: try: s.connect((host, port)) except socket.error: if reraise: raise return False else: if not handshake: return True # See if it smells like ZEO try: data = s.recv(1024) except socket.error: if reraise: raise return False else: return bool(SERVER_PROTOCOL.search(data)) finally: s.close() return True if __name__ == '__main__': import sys print zeo_ping(*sys.argv[1:])