示例#1
0
 def test_update_to_string_without_time(self):
     statistic = TcpStatistic(self.host, self.port, list())
     self.assertEqual((self.host, self.port), statistic.statistic_by())
     self.assertEqual(
         statistic.update_to_string(PingRequest(self.host, self.port, -1)),
         f'Probing {self.host}:{self.port}'
         f'/tcping - No response')
示例#2
0
 def test_update_to_string(self):
     statistic = TcpStatistic(self.host, self.port, list())
     self.assertEqual((self.host, self.port), statistic.statistic_by())
     self.assertEqual(
         statistic.update_to_string(
             PingRequest(self.host, self.port, self.time)),
         f'Probing {self.host}:{self.port}/tcping - '
         f'Port is open - time={self.time*1000}ms')
示例#3
0
    def test_update_to_string_with_jitter(self):
        statistic = TcpStatistic(self.host, self.port, list())
        self.assertEqual((self.host, self.port), statistic.statistic_by())

        self.assertEqual(
            statistic.update_to_string(
                PingRequest(self.host, self.port, self.time), self.jitter),
            f'Probing {self.host}:{self.port}'
            f'/tcping - Port is open - '
            f'time={self.time*1000}ms '
            f'jitter={self.jitter}ms')
示例#4
0
 def test_get_empty_statistic(self):
     statistic = TcpStatistic(self.host, self.port, [
         PingRequest(self.host, self.port, -1),
         PingRequest(self.host, self.port, -1)
     ])
     self.assertEqual(
         statistic.get_statistic(),
         f'Probing {self.host}:{self.port}/tcping - '
         f'No response\n'
         'Probing localhost:5001/tcping - No response\n'
         f'\nPing statistics for {self.host}:{self.port}\n'
         '\t2 probes sent.\n'
         '\t0 successful, 2 failed.\n'
         'Was unable to connect, cannot provide trip statistics.')
示例#5
0
 def test_get_statistic(self):
     statistic = TcpStatistic(self.host, self.port, [
         PingRequest(self.host, self.port, 0.001),
         PingRequest(self.host, self.port, 0.002)
     ])
     self.assertEqual(
         statistic.get_statistic(),
         f'Probing {self.host}:{self.port}/tcping - '
         f'Port is open - time=1.0ms\n'
         'Probing localhost:5001/tcping - '
         'Port is open - time=2.0ms\n'
         f'\nPing statistics for {self.host}:{self.port}\n'
         '\t2 probes sent.\n'
         '\t2 successful, 0 failed.\n'
         'Approximate trip times in milli-seconds:\n'
         '\tMinimum = 1.0ms, Maximum = 2.0ms, '
         'Average = 1.500ms')
示例#6
0
def main(args):
    try:
        args.host = socket.gethostbyname(args.host)
    except socket.gaierror:
        raise TCPConnectionError(args.host, args.port)
    tcping = TCPing(host=args.host,
                    port=args.port,
                    pause=args.sleep_time,
                    wait=args.wait,
                    ipv6=args.ipv6)
    args.count = args.count if not args.cycle else None
    tcp_result = tcping.ping_with_count(args.count)
    statistic = TcpStatistic(host=args.host,
                             port=args.port,
                             pings=tcp_result,
                             jitter=args.jitter,
                             date=args.date)
    stat_result = statistic.get_statistic()
    if args.path:
        with open(args.path, 'a') as file:
            print(stat_result, file=file)
    else:
        print(stat_result)
示例#7
0
 def __init__(self, host: str, port: int):
     self.host = socket.gethostbyname(host)
     self.statistic = TcpStatistic(self.host, port)
     self.port = port
示例#8
0
 def test_exception(self):
     statistic = TcpStatistic(self.host, self.port, [1, 2, 3])
     with self.assertRaises(TypeError):
         statistic.get_statistic()
示例#9
0
 def test_update_time(self):
     self.assertEqual(TcpStatistic()._update_time(0.001), 1.0)
     self.assertEqual(TcpStatistic()._update_time(0.00132), 1.32)
     self.assertEqual(TcpStatistic()._update_time(0.0132), 13.2)
     self.assertEqual(TcpStatistic()._update_time(13.2, kf=1), 13.2)