Python Forum
TCP ping time mesure diffrence from other tools
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TCP ping time mesure diffrence from other tools
#1
Hello,

I am trying to write a simple tool that measures the TCP port open, the code works well.
The issue I have is that when I compare the TCP ping result to other tools, such psping (Sysinternals), tcpping , check_tcp(Nagios), I get much lower results than my code.
In my code open TCP port open takes around 9~10ms, others  tools takes 0.200~0.500ms
in the beginning, thought its related to the case if I have multiple interfaces on my computer, I added to bind to the correct interface and didn't change the results.
My question is, why I get such different results?
Please advice
Thanks

#!/usr/bin/env python3
"""
TCP Ping Test (defaults to port 80, 10000 packets)

Usage: ./tcpping.py host [port] [maxCount]
- Ctrl-C Exits with Results
"""

import sys
import socket
import time
import signal
from timeit import default_timer as timer

host = None
port = 80
a = time.clock()
time.sleep(2)
b = time.clock()
print(b-a)
# Default to 10000 connections max
maxCount = 10000
count = 0

## Inputs

# Required Host
try:
    host = sys.argv[1]
except IndexError:
    print("Usage: tcpping.py host [port] [maxCount]")
    sys.exit(1)

# Optional Port
try:
    port = int(sys.argv[2])
except ValueError:
    print("Error: Port Must be Integer:", sys.argv[3])
    sys.exit(1)
except IndexError:
    pass

# Optional maxCount
try:
    maxCount = int(sys.argv[3])
except ValueError:
    print("Error: Max Count Value Must be Integer", sys.argv[3])
    sys.exit(1)
except IndexError:
    pass

# Pass/Fail counters
passed = 0
failed = 0


def getResults():
    """ Summarize Results """

    lRate = 0
    if failed != 0:
        lRate = failed / (count) * 100
        lRate = "%.2f" % lRate

    print("\nTCP Ping Results: Connections (Total/Pass/Fail): [{:}/{:}/{:}] (Failed: {:}%)".format((count), passed, failed, str(lRate)))

def signal_handler(signal, frame):
    """ Catch Ctrl-C and Exit """
    getResults()
    sys.exit(0)

# Register SIGINT Handler
signal.signal(signal.SIGINT, signal_handler)

# Loop while less than max count or until Ctrl-C caught
while count < maxCount:

    # Increment Counter
    count += 1

    success = False

    # New Socket
    s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('192.168.1.19', 0))

    # 1sec Timeout
    s.settimeout(1)


    # Try to Connect
    try:
        # Start a timer
        s_start = timer()
        s.connect((host, int(port)))
        # Stop Timer
        s_stop = timer()
        s.shutdown(socket.SHUT_RD)
        success = True

    # Connection Timed Out
    except socket.timeout:
        print("Connection timed out!")
        failed += 1
    except OSError as e:
        print("OS Error:", e)
        failed += 1

    # Stop Timer
    #s_stop = timer()
    s_runtime = "%.2f" % (1000 * (s_stop - s_start))

    if success:
        print("Connected to %s[%s]: tcp_seq=%s time=%s ms" % (host, port, (count-1), s_runtime))
        passed += 1

    # Sleep for 1sec
    if count < maxCount:
        time.sleep(1)

# Output Results if maxCount reached
getResults()
Reply
#2
Hi,

I think I know why it have such difference between  python and other tools, I used Wireshark to capture both python and psping, and I found that
 the final handshake from client side FIN, ACK is much slower in Python than psping and that is the difference the 10ms.
Is there any flag in Python TCP  to make it faster?
Thanks
[Image: y4mUta44j2aIPOfkpIQ-rW7Fv5yaH2IcOHFtmD_E...pmode=none]
[Image: y4m2B5B0cfmJNbnHMdxnw3tLxczEKK-ISA8Agfn_...pmode=none]
Reply
#3
Try Scapy.  It's quite easy to send ICMP packets with Scapy.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mesure delay time in network (graph) leemao 0 4,756 Jul-02-2020, 04:06 PM
Last Post: leemao
  Need to build a ping test for work network austina2 2 3,476 Mar-11-2019, 06:58 PM
Last Post: austina2
  ping program in python Skaperen 2 3,247 Jun-23-2018, 03:32 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020