On Mon, Jun 27, 2005 at 02:38:48PM -0400, Tim Peters wrote:
| > Any suggestions about how to best implement this,
| 
| Maybe just create your own socket, attempt to connect, and if that attempt
| fails produce the kind of error message you want to see?  Of course if it
| succeeds, there's some chance it will fail if you immediately close the
| socket and do a "normal" connection attempt.

That is good enough for me yes. Attached the script I've came up with,
which is far from perfect but has just the amount of functionality I
needed. Maybe we should include something similar to that with ZEO
itself?

-- 
Sidnei da Silva
Enfold Systems, LLC.
#!/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:])
_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to