[Checkins] SVN: zc.zk/trunk/src/zc/zk/ If a server is registered with an empty host name, the hostname is

Jim Fulton jim at zope.com
Mon Dec 12 22:01:57 UTC 2011


Log message for revision 123784:
  If a server is registered with an empty host name, the hostname is
  changed to the result of `socket.getfqdn()``.
  

Changed:
  U   zc.zk/trunk/src/zc/zk/README.txt
  U   zc.zk/trunk/src/zc/zk/__init__.py
  U   zc.zk/trunk/src/zc/zk/tests.py

-=-
Modified: zc.zk/trunk/src/zc/zk/README.txt
===================================================================
--- zc.zk/trunk/src/zc/zk/README.txt	2011-12-12 21:35:10 UTC (rev 123783)
+++ zc.zk/trunk/src/zc/zk/README.txt	2011-12-12 22:01:57 UTC (rev 123784)
@@ -547,6 +547,20 @@
 It would be bad, in practice, to remove a node that processes are
 watching.
 
+Registering a server with a blank hostname
+==========================================
+
+It's common to use an empty string for a host name when calling bind
+to listen on all IPv4 interfaces.  If you pass an empty host name to
+``register_server``, the result of calling ``socket.getfqdn()`` will
+be registered:
+
+    >>> zk.register_server('/fooservice/providers', ('', 42))
+    addresses changed
+    ['192.168.0.42:8080', '192.168.0.42:8081', '192.168.0.42:8082',
+     'server.example.com:42']
+
+
 Server-registration events
 ==========================
 
@@ -598,6 +612,8 @@
           pid = 7981
         /192.168.0.42:8082
           pid = 7981
+        /server.example.com:42
+          pid = 7981
 
 .. -> sh
 
@@ -875,10 +891,12 @@
 Change History
 ==============
 
-0.4.0 (2011-12-??)
+0.4.0 (2011-12-12)
 ------------------
 
 - Provided a command-line tool, ``zookeeper_export``,  to export/print trees.
+- If a server is registered with an empty host name, the hostname is
+  changed to the result of `socket.getfqdn()``.
 - Fixed a race that could cause ZooKeeper logging info to be output
   before ``zc.zk`` began redirecting it.
 

Modified: zc.zk/trunk/src/zc/zk/__init__.py
===================================================================
--- zc.zk/trunk/src/zc/zk/__init__.py	2011-12-12 21:35:10 UTC (rev 123783)
+++ zc.zk/trunk/src/zc/zk/__init__.py	2011-12-12 22:01:57 UTC (rev 123784)
@@ -16,6 +16,7 @@
 import logging
 import os
 import re
+import socket
 import sys
 import threading
 import time
@@ -176,7 +177,12 @@
 
     def register_server(self, path, addr, acl=READ_ACL_UNSAFE, **kw):
         kw['pid'] = os.getpid()
-        if not isinstance(addr, str):
+        if isinstance(addr, str):
+            if addr[:1] == ':':
+                addr = socket.getfqdn()+addr
+        else:
+            if addr[0] == '':
+                addr = socket.getfqdn(), addr[1]
             addr = '%s:%s' % addr
         path = self.resolve(path)
         zc.zk.event.notify(RegisteringServer(addr, path, kw))

Modified: zc.zk/trunk/src/zc/zk/tests.py
===================================================================
--- zc.zk/trunk/src/zc/zk/tests.py	2011-12-12 21:35:10 UTC (rev 123783)
+++ zc.zk/trunk/src/zc/zk/tests.py	2011-12-12 22:01:57 UTC (rev 123784)
@@ -1054,6 +1054,13 @@
     test.globs['check_async'] = check_async
     test.globs['event'] = event
 
+def setUpREADME(test):
+    zc.zk.testing.setUp(test)
+    cm = mock.patch('socket.getfqdn')
+    m = cm.__enter__()
+    m.side_effect = lambda : 'server.example.com'
+    test.globs['zc.zk.testing'].append(cm.__exit__)
+
 checker = zope.testing.renormalizing.RENormalizing([
     (re.compile('pid = \d+'), 'pid = 9999'),
     (re.compile("{'pid': \d+}"), 'pid = 9999'),
@@ -1064,9 +1071,11 @@
 def test_suite():
     suite = unittest.TestSuite((
         manuel.testing.TestSuite(
-            manuel.doctest.Manuel(checker=checker) + manuel.capture.Manuel(),
+            manuel.doctest.Manuel(
+                checker=checker, optionflags=doctest.NORMALIZE_WHITESPACE) +
+            manuel.capture.Manuel(),
             'README.txt',
-            setUp=zc.zk.testing.setUp, tearDown=zc.zk.testing.tearDown,
+            setUp=setUpREADME, tearDown=zc.zk.testing.tearDown,
             ),
         doctest.DocTestSuite(
             setUp=zc.zk.testing.setUp, tearDown=zc.zk.testing.tearDown,



More information about the checkins mailing list