Created
April 15, 2020 03:43
-
-
Save 0x2a94b5/c9401198e9a29065682af00c5e4a1196 to your computer and use it in GitHub Desktop.
Network Latency For Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!.venv/bin/python3.6 | |
# -*- coding:utf-8 -*- | |
# | |
# Network Latency For Windows | |
# | |
import sys | |
from re import findall | |
from subprocess import Popen, PIPE | |
# pip install validators=0.14.3 | |
from validators import domain, ip_address | |
def is_domain(host): | |
if domain(host): | |
return True | |
return False | |
def is_ip(host): | |
if ip_address.ipv4(host): | |
return True | |
return False | |
def main(host): | |
try: | |
ping = Popen(["ping.exe", '-w', '3', host], stdout = PIPE) | |
output = ping.communicate()[0] | |
pattern = r"平均 = (\d+\S+)" | |
result = findall(pattern, output.decode(encoding='gbk'))[0] | |
except Exception as e: | |
print("Network Error:", e) | |
return False | |
return result | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("python get_latency.py ip|domain") | |
sys.exit(-1) | |
host = sys.argv[1] | |
if is_ip(host) or is_domain(host): | |
times = main(host) | |
if times: | |
print("Network Latency:" + times) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment