Created
December 3, 2014 21:53
-
-
Save iserko/7a79f78d856a7f45a573 to your computer and use it in GitHub Desktop.
simple TCP connect test script
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
#!/usr/bin/env python | |
import socket | |
import sys | |
import time | |
def run(): | |
host = sys.argv[1] | |
port = int(sys.argv[2]) | |
iteration = 0 | |
failed = 0 | |
results = [] | |
while True: | |
try: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.settimeout(1.0) | |
start_time = time.time() | |
try: | |
sock.connect((host, port)) | |
end_time = time.time() | |
diff_time = (end_time - start_time) * 1000 | |
except socket.timeout: | |
print " - CONNECT to %s:%s (#%s) timed out" % (host, port, iteration) | |
failed += 1 | |
continue | |
finally: | |
sock.close() | |
iteration += 1 | |
print " - CONNECT to %s:%s (#%s) took %.1f ms" % (host, port, iteration, diff_time) | |
results.append(diff_time) | |
time.sleep(0.1) | |
except KeyboardInterrupt: | |
print "" | |
print ("%s CONNECTs (%s failed) %.1f%% success rate to %s:%s. min/avg/max = " | |
"%.1f/%.1f/%.1f ms") % ( | |
iteration, failed, (float(iteration - failed) / iteration * 100), host, port, | |
min(results), (float(sum(results)) / float(len(results))), | |
max(results)) | |
break | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment