Tcp ping in python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

NitroLine/tcping

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

usage: tcpping.py [-h] [-c MAX_COUNT] [-6] [-t TIME] HOST PORT [PORT . ] positional arguments: HOST Host for tcp ping PORT Port for tcp ping (default=80) optional arguments: -h, --help show this help message and exit -c MAX_COUNT, --count MAX_COUNT max count of ping request (default=10000) -6, --ipv6 flag for use IPv6 address -t TIME, --timeout TIME ping timeout in seconds (default=1) 

python3 tcping.py 8.8.8.8 53 443 80

python3 tcping.py -6 fe80::24d4:1fee:fa42:9d2d 5566 -t 5

Источник

Ping¶

Verifies whether a TCP port is open on a given IP address.

An IP address in string format that is able to be converted by ipaddress library.

The timeout in seconds before returning a False. Defaults to 1.

The result as to whether or not you were able ping the IP address.

 >>> from netutils.ping import tcp_ping >>> tcp_ping("1.1.1.1", 443) True >>> tcp_ping("1.0.100.0", 27) False >>> 
 5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 def tcp_ping(ip: str, port: int, timeout: int = 1) -> bool: # pylint: disable=invalid-name """Verifies whether a TCP port is open on a given IP address. Args: ip: An IP address in string format that is able to be converted by `ipaddress` library. port: A valid TCP port. timeout: The timeout in seconds before returning a False. Defaults to 1. Returns: The result as to whether or not you were able ping the IP address. Examples: >>> from netutils.ping import tcp_ping >>> tcp_ping("1.1.1.1", 443) # doctest: +SKIP True >>> tcp_ping("1.0.100.0", 27) # doctest: +SKIP False >>> """ sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sckt.settimeout(int(timeout)) try: sckt.connect((ip, int(port))) # pylint: disable=invalid-name sckt.shutdown(socket.SHUT_RDWR) return True # We really only want to know if the TCP connection timed out. # If anything else has happened the error should be raised. except socket.timeout: return False finally: sckt.close() 

Источник

Читайте также:  Jenkins execute python script

serviceping

A utility with a «ping» like interface to ping tcp port services.

Table of Contents

Background

This utility was written to simplify troubleshooting network issues related to talking to network services

Usage

Serviceping provides a command line interface that operates like the ping command but instead of using icmp packets to check for a response from a host. It can perform a tcp network connection to a port on a host or a http or https get request to check a url on a host.

Since tcp and http requests require multiple operations. Each request performs all of the operations end to end for the request. The serviceping command adds a -d flag that will show timings for the different stages the ping request.

usage: serviceping [-h] [-c COUNT] [-i INTERVAL] [-W TIMEOUT] [-d] destination [destination . ] positional arguments: destination Destination host or URL optional arguments: -h, --help show this help message and exit -c COUNT Number of pings to send -i INTERVAL Ping interval -d Show timings for the entire connection -W TIMEOUT Time to wait for a response, in seconds. The option affects only timeout in absence of any responses 

Examples

The serviceping tool uses a syntax that mirrors that of the ping commmand.

Ping port 80 (http) on www.yahoo.com

By pinging www.yahoo.com via http (port 80), we can clearly see the multiple hosts responding and the latency of each request.

Serviceping can also connect to other ports such as the ssl port (443).

 serviceping www.yahoo.com:443      

The serviceping command can also specify send ping requests to a url. If a URL is specified, it will perform an http get request and show the response, which is useful when hosts are doing unexpected things in a dns rotation or behind a reverse proxy or vip.

In this example we specify a url of http://cnn.com/

 serviceping http://cnn.com/        The output shows that two hosts are responding to this request, and that they are returning different amounts of data in their responses.

Pinging a URL with timings

The detailed timing flag adds timings for each step of each request, which is useful for determining the causes of latency issues or errors.

Here we are doing the previous example with detailed timings.

 serviceping -d http://cnn.com/        Clearly, the host with address 157.166.255.18 is taking significantly longer to establish the tcp connection and handle the http request.

Pinging a SSL URL with timings

When pinging a ssl URL serviceping adds the ssl version information in the response.

 serviceping -d https://www.cnn.com/        This project is licensed under the terms of the Apache 2.0 open source license. Please refer to LICENSE for the full terms.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Dead Simple Python TCP Ping Tool

License

yantisj/tcpping

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Dead simple TCP Ping tool written in Python3. Establishes one connection per second, times out after one second, and defaults to 10000 connections unless interrupted with Ctrl-C.

$ ./tcpping.py Usage: tcpping.py host [port] [maxCount] 
./tcpping.py 10.33.1.100 22 Connection timed out! Connected to 10.33.1.100:22 via TCP: tcp_seq=1 time=39.66 ms Connected to 10.33.1.100:22 via TCP: tcp_seq=2 time=20.41 ms Connected to 10.33.1.100:22 via TCP: tcp_seq=3 time=85.18 ms ^C TCP Ping Results: Connections (Total/Pass/Fail): [4/3/1] (Failed: 25%) 

tcpping is licensed under the GNU AGPLv3 License.

Источник

How to ping a server url/port?

How do I ping a server via the url / port in python, and receive the response in ms (milliseconds)? I’m beating my head, I’ve used tcping , and I’m studying sockets, but it doesn’t seem so simple, any tips? pings_ookla_script_root-OK.py :

#!/usr/bin/env python3 import requests import os import sys import pings p = pings.Ping(quiet=False) port = 8080 response = p.ping('google.com', times=4) print(response) 
47 bytes from 142.250.78.206: icmp_seq=0 ttl=114 time=11.326 ms 47 bytes from 142.250.78.206: icmp_seq=1 ttl=114 time=11.059 ms 47 bytes from 142.250.78.206: icmp_seq=2 ttl=114 time=10.793 ms 47 bytes from 142.250.78.206: icmp_seq=3 ttl=114 time=11.213 ms --- google.com ping statistics --- 4 packets transmitted, 4 packets received, 0.0% packet loss round-trip min/avg/max = 10.793/11.098/11.326 ms

You don’t ping a port, you ping a host. Ping is a protocol defined on Internet layer, rather than on the Transport layer. See Internet protocol suite

I think the pings module already returns the response in milliseconds. It looks like it is in response.avg_rtt . To get rid of the other output do p = pings.Ping(quiet=True) so you better see what is returned and what not.

OK! But how do I know my server’s response time? Is there any other way? I’ve tried tcping and I couldn’t evolve, any tips?

If your server is running at home, you have to ask some friends to ping it. Otherwise you can’t see how fast it is. Best would be if you have friends from all over the world pinging your server. You also could use a v-server or anything that can run the ping command from somewhere else via remote connection. On the other hand, nowadays the upload speed is similar to the download speed. If you ping something on the web like google, you pretty much got the speed of your server — 11 milliseconds currently. But clients on other continents might be much worse. It depends on the client location.

Hi areap-enap, Thank you for the tips but it is a web server that is only accessible via the port, eg jardel.com:8080, and I wanted to know its response time. I get this answer through zabbix, but I wanted to find out via python script

1 Answer 1

You can’t ping ports, as Ping is using ICMP which doesn’t have the concept of ports. Ports belong to the transport layer protocols like TCP and UDP.

If you want do web server test that is only accessible via the port,you can use module tcp_latency

tcp-latency provides an easy way to measure latency using TCP.
Inspired by other similar tools, tcp-latency comes from the need of running network diagnosis/troubleshooting tasks with Python on serverless infrastructure (as many providers don’t include ping/ICMP support) but should work too in any other environment with Python>=36.
Features
Runs as a command-line tool or inside your code as a module. Custom parameters for a port, runs, timeout and wait time between runs. IPv4 (e.g 52.26.14.11) and domain (e.g google.com) host support. Human readable output when running as a command-line tool. No external dependencies. Small and extensible.

pip3 install tcp_latency python3 

now you are in python env.

>>> from tcp_latency import measure_latency >>> measure_latency(host='google.com') [34.57] >>> measure_latency(host='52.26.14.11', port=80, runs=10, timeout=2.5) [433.82, 409.21, 409.25, 307.09, 306.64, 409.45, 306.58, 306.93, 409.25, 409.26] 

Источник

Оцените статью