@@ -818,7 +818,7 @@ def run(self):
818818 rr , wr , er = select .select (rs , [], [], self .timeout )
819819 for socket_ in rr :
820820 try :
821- self .readers [socket_ ].handle_read ()
821+ self .readers [socket_ ].handle_read (socket_ )
822822 except Exception as e : # TODO stop catching all Exceptions
823823 log .exception ('Unknown error, possibly benign: %r' , e )
824824 except Exception as e : # TODO stop catching all Exceptions
@@ -856,11 +856,10 @@ class Listener(object):
856856
857857 def __init__ (self , zc ):
858858 self .zc = zc
859- self .zc .engine .add_reader (self , self .zc .socket )
860859
861- def handle_read (self ):
860+ def handle_read (self , socket_ ):
862861 try :
863- data , (addr , port ) = self . zc . socket .recvfrom (_MAX_MSG_ABSOLUTE )
862+ data , (addr , port ) = socket_ .recvfrom (_MAX_MSG_ABSOLUTE )
864863 except socket .error as e :
865864 # If the socket was closed by another thread -- which happens
866865 # regularly on shutdown -- an EBADF exception is thrown here.
@@ -1199,8 +1198,8 @@ def __init__(self):
11991198 multicast communications, listening and reaping threads."""
12001199 global _GLOBAL_DONE
12011200 _GLOBAL_DONE = False
1202- self .socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
1203- self .socket .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
1201+ self ._socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
1202+ self ._socket .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
12041203
12051204 # SO_REUSEADDR should be equivalent to SO_REUSEPORT for
12061205 # multicast UDP sockets (p 731, "TCP/IP Illustrated,
@@ -1212,19 +1211,19 @@ def __init__(self):
12121211 except AttributeError :
12131212 pass
12141213 else :
1215- self .socket .setsockopt (socket .SOL_SOCKET , reuseport , 1 )
1214+ self ._socket .setsockopt (socket .SOL_SOCKET , reuseport , 1 )
12161215
1217- self .socket .setsockopt (socket .IPPROTO_IP , socket .IP_MULTICAST_TTL , 255 )
1218- self .socket .setsockopt (socket .IPPROTO_IP , socket .IP_MULTICAST_LOOP , 1 )
1216+ self ._socket .setsockopt (socket .IPPROTO_IP , socket .IP_MULTICAST_TTL , 255 )
1217+ self ._socket .setsockopt (socket .IPPROTO_IP , socket .IP_MULTICAST_LOOP , 1 )
12191218 try :
1220- self .socket .bind (('' , _MDNS_PORT ))
1219+ self ._socket .bind (('' , _MDNS_PORT ))
12211220 except Exception as e : # TODO stop catching all Exceptions
12221221 # Some versions of linux raise an exception even though
12231222 # the SO_REUSE* options have been set, so ignore it
12241223 #
12251224 log .exception ('Unknown error, possibly benign: %r' , e )
1226- self .socket .setsockopt (socket .IPPROTO_IP , socket .IP_ADD_MEMBERSHIP ,
1227- socket .inet_aton (_MDNS_ADDR ) + socket .inet_aton ('0.0.0.0' ))
1225+ self ._socket .setsockopt (socket .IPPROTO_IP , socket .IP_ADD_MEMBERSHIP ,
1226+ socket .inet_aton (_MDNS_ADDR ) + socket .inet_aton ('0.0.0.0' ))
12281227
12291228 self .listeners = []
12301229 self .browsers = []
@@ -1237,6 +1236,7 @@ def __init__(self):
12371236
12381237 self .engine = Engine (self )
12391238 self .listener = Listener (self )
1239+ self .engine .add_reader (self .listener , self ._socket )
12401240 self .reaper = Reaper (self )
12411241
12421242 def wait (self , timeout ):
@@ -1514,7 +1514,7 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT):
15141514 packet = out .packet ()
15151515 try :
15161516 while packet :
1517- bytes_sent = self .socket .sendto (packet , 0 , (addr , port ))
1517+ bytes_sent = self ._socket .sendto (packet , 0 , (addr , port ))
15181518 if bytes_sent < 0 :
15191519 break
15201520 packet = packet [bytes_sent :]
@@ -1531,7 +1531,7 @@ def close(self):
15311531 self .notify_all ()
15321532 self .engine .notify ()
15331533 self .unregister_all_services ()
1534- self .socket .close ()
1534+ self ._socket .close ()
15351535
15361536# Test a few module features, including service registration, service
15371537# query (for Zoe), and service unregistration.
0 commit comments