zeroconf: guess ip for Zeroconf
authorAlexander Solovyov <piranha@piranha.org.ua>
Mon, 27 Apr 2009 21:33:39 +0300
changeset 8264 63ea850b3312
parent 8263 41031699550a
child 8265 52c5be55af82
zeroconf: guess ip for Zeroconf Zeroconf() is actually pretty dumb in guessing IPs and in case of socket.gaierror will not guess right IP.
hgext/zeroconf/__init__.py
--- a/hgext/zeroconf/__init__.py	Wed Apr 29 20:47:30 2009 -0500
+++ b/hgext/zeroconf/__init__.py	Mon Apr 27 21:33:39 2009 +0300
@@ -51,9 +51,12 @@
         pass
 
     # Generic method, sometimes gives useless results
-    dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
-    if not dumbip.startswith('127.') and ':' not in dumbip:
-        return dumbip
+    try:
+        dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
+        if not dumbip.startswith('127.') and ':' not in dumbip:
+            return dumbip
+    except socket.gaierror:
+        dumbip = '127.0.0.1'
 
     # works elsewhere, but actually sends a packet
     try:
@@ -69,13 +72,12 @@
 def publish(name, desc, path, port):
     global server, localip
     if not server:
-        try:
-            server = Zeroconf.Zeroconf()
-        except socket.gaierror:
+        ip = getip()
+        if ip.startswith('127.'):
             # if we have no internet connection, this can happen.
             return
-        ip = getip()
         localip = socket.inet_aton(ip)
+        server = Zeroconf.Zeroconf(ip)
 
     hostname = socket.gethostname().split('.')[0]
     host = hostname + ".local"
@@ -129,7 +131,10 @@
         self.found[repr(name)] = server.getServiceInfo(type, name)
 
 def getzcpaths():
-    server = Zeroconf.Zeroconf()
+    ip = getip()
+    if ip.startswith('127.'):
+        return
+    server = Zeroconf.Zeroconf(ip)
     l = listener()
     Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
     time.sleep(1)