def __enter__(self): # Actually we DROP the return traffic rather than the ougoing # traffic. Dropping the outgoing traffic has different issues on # different platforms. For example a local ICMP unreachable may # be generated or existing connections may not be impacted. if sys.platform == 'darwin': command = 'ipfw -q add 2000 drop tcp from {0} to any' \ .format(self.ipaddr) elif sys.platform in ('linux2', 'linux3'): command = 'iptables -I INPUT 1 -s {0} -j DROP' \ .format(self.ipaddr) sudo.run_with_sudo(command.split())
def __exit__(self, *exc): if sys.platform == 'darwin': command = 'ipfw -q del 2000' elif sys.platform in ('linux2', 'linux3'): command = 'iptables -D INPUT 1' sudo.run_with_sudo(command.split())