Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_verbose_ping_normal(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com")
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")
def test_verbose_ping_ttl_exception(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com", ttl=1)
self.assertRegex(fake_out.getvalue(), r".*Timeout.*")
def test_verbose_ping_ttl(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com", ttl=1)
self.assertRegex(fake_out.getvalue(), r".*Timeout.*")
def test_verbose_ping_timeout(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com", timeout=0.0001)
self.assertRegex(fake_out.getvalue(), r".*Timeout \> [0-9\.]+s.*")
def test_verbose_ping_timeout_exception(self):
with patch("sys.stdout", new=io.StringIO()):
with patch("ping3.EXCEPTIONS", True):
with self.assertRaises(errors.Timeout):
ping3.verbose_ping("example.com", timeout=0.0001)
def test_verbose_ping_count(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com", count=1)
self.assertEqual(fake_out.getvalue().count("\n"), 1)
def test_verbose_ping_bind(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
my_ip = socket.gethostbyname(socket.gethostname())
dest_addr = "example.com"
if my_ip == "127.0.0.1" or my_ip == "127.0.1.1": # This may caused by /etc/hosts settings.
dest_addr = my_ip # only localhost can send and receive from 127.0.0.1 (or 127.0.1.1 on Ubuntu).
ping3.verbose_ping(dest_addr, src_addr=my_ip)
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")
def test_verbose_ping_size(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
ping3.verbose_ping("example.com", size=100)
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")
with self.assertRaises(OSError):
ping3.ping("example.com", size=99999)
"""
parser = argparse.ArgumentParser(prog="ping3", description="A pure python3 version of ICMP ping implementation using raw socket.", epilog="!!Note: ICMP messages can only be sent from processes running as root.")
parser.add_argument("-v", "--version", action="version", version=ping3.__version__)
parser.add_argument(dest="dest_addr", metavar="DEST_ADDR", nargs="*", default=("example.com", "8.8.8.8"), help="The destination address, can be an IP address or a domain name. Ex. 192.168.1.1/example.com.")
parser.add_argument("-c", "--count", dest="count", metavar="COUNT", type=int, default=4, help="How many pings should be sent. Default is 4.")
parser.add_argument("-w", "--wait", dest="timeout", metavar="TIMEOUT", type=float, default=4, help="Time to wait for a response, in seconds. Default is 4.")
parser.add_argument("-t", "--ttl", dest="ttl", metavar="TTL", type=int, default=64, help="The Time-To-Live of the outgoing packet. Default is 64.")
parser.add_argument("-l", "--load", dest="size", metavar="SIZE", type=int, default=56, help="The ICMP packet payload size in bytes. Default is 56.")
parser.add_argument("--debug", action="store_true", dest="debug", help="Turn on DEBUG mode.")
parser.add_argument("--exceptions", action="store_true", dest="exceptions", help="Turn on EXCEPTIONS mode.")
args = parser.parse_args(assigned_args)
ping3.DEBUG = True if args.debug else False
ping3.EXCEPTIONS = True if args.exceptions else False
for addr in args.dest_addr:
ping3.verbose_ping(addr, count=args.count, ttl=args.ttl, timeout=args.timeout, size=args.size)