def handle_close(self):
try:
delete_set = self.getgrouplist()
for multicast_addr in delete_set:
self.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP,
socket.inet_aton(multicast_addr) + socket.inet_aton('0.0.0.0'))
if self.callback_obj is not None:
self.callback_obj.on_leave(self, multicast_addr)
with self.lock:
self.multicastSet = Set([])
except Exception as e:
print e
print 'asyncUdp close called'
asyncore.dispatcher.close(self)
AsyncController.instance().discard(self)
try:
if self.callback_obj is not None:
self.callback_obj.on_stopped(self)
except Exception as e:
print e
traceback.print_exc()
# noinspection PyMethodOverriding
python类IP_DROP_MEMBERSHIP的实例源码
def _joinAddr2(self, interface, addr, join):
addr = socket.inet_aton(addr)
interface = socket.inet_aton(interface)
if join:
cmd = socket.IP_ADD_MEMBERSHIP
else:
cmd = socket.IP_DROP_MEMBERSHIP
try:
self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
except socket.error, e:
return failure.Failure(error.MulticastJoinError(addr, interface, *e.args))
def _joinAddr2(self, interface, addr, join):
addr = socket.inet_aton(addr)
interface = socket.inet_aton(interface)
if join:
cmd = socket.IP_ADD_MEMBERSHIP
else:
cmd = socket.IP_DROP_MEMBERSHIP
try:
self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
except socket.error, e:
return failure.Failure(error.MulticastJoinError(addr, interface, *e.args))
def close(self):
"""Ends the background threads, and prevent this instance from
servicing further queries."""
if globals()['_GLOBAL_DONE'] == 0:
globals()['_GLOBAL_DONE'] = 1
self.notifyAll()
self.engine.notify()
self.unregisterAllServices()
self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
self.socket.close()
# Test a few module features, including service registration, service
# query (for Zoe), and service unregistration.
def run(self):
# XXX: why does self.server_bind() fail???
## import pdb; pdb.set_trace()
try:
# bind and activate the server
self.server_bind()
self.server_activate()
except:
# close the server
self.server_close()
# propagate the error
raise
try:
# join the multicast group
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self._group_member)
# process requests until stopped
self.serve_forever()
finally:
# leave the multicast group
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, self._group_member)
# close the server
self.server_close()
def _joinAddr2(self, interface, addr, join):
addr = socket.inet_aton(addr)
interface = socket.inet_aton(interface)
if join:
cmd = socket.IP_ADD_MEMBERSHIP
else:
cmd = socket.IP_DROP_MEMBERSHIP
try:
self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
except socket.error as e:
return failure.Failure(error.MulticastJoinError(addr, interface,
*e.args))
def _joinAddr2(self, interface, addr, join):
addr = socket.inet_aton(addr)
interface = socket.inet_aton(interface)
if join:
cmd = socket.IP_ADD_MEMBERSHIP
else:
cmd = socket.IP_DROP_MEMBERSHIP
try:
self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
except socket.error as e:
return failure.Failure(error.MulticastJoinError(addr, interface, *e.args))
def leave(self, multicast_addr):
with self.lock:
try:
if multicast_addr in self.multicastSet:
self.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP,
socket.inet_aton(multicast_addr) + socket.inet_aton('0.0.0.0'))
self.multicastSet.discard(multicast_addr)
if self.callback_obj is not None:
self.callback_obj.on_leave(self, multicast_addr)
except Exception as e:
print e