Last active
October 5, 2018 04:07
-
-
Save yc0/e5fa0a7c589d8827f39e4158bc8d58e8 to your computer and use it in GitHub Desktop.
ping-pong tcp testing for network benchmark
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
import socket | |
import datetime | |
import sys | |
import time | |
def main(): | |
message = "Ping" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.settimeout(120) | |
if len(sys.argv) != 3: | |
print "Correct usage: script, IP address, port number" | |
exit() | |
IP_address = str(sys.argv[1]) | |
Port = int(sys.argv[2]) | |
sock.connect((IP_address, Port)) | |
ctr = 300 | |
i = ctr | |
rst = [] | |
while i > 0: | |
time.sleep(1) | |
#print('start...') | |
try: | |
begin_time = datetime.datetime.now() | |
sock.send(message) | |
while True: | |
#print "waiting..." | |
data = sock.recv(2048) | |
if data == "Pong": | |
end_time = datetime.datetime.now() | |
print("recv at {}".format(end_time)) | |
i = i - 1 | |
break | |
else: | |
print(data) | |
continue | |
except socket.timeout: | |
print "time out" | |
else: | |
msec = (end_time - begin_time).microseconds / 1000.0 | |
#print "rtt: %f msec" % msec | |
rst.append(msec) | |
print "avg rtt: %f msec, max rtt %f msec, min rtt %f msec at %d rounds" % (sum(rst)/len(rst), max(rst), min(rst),ctr) | |
sock.close() | |
if __name__ == '__main__': | |
main() |
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
import socket | |
import sys | |
def serve_forever(): | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
# checks whether sufficient arguments have been provided | |
if len(sys.argv) != 3: | |
print "Correct usage: script, IP address, port number" | |
exit() | |
# takes the first argument from command prompt as IP address | |
IP_address = str(sys.argv[1]) | |
# takes second argument from command prompt as port number | |
Port = int(sys.argv[2]) | |
""" | |
binds the server to an entered IP address and at the | |
specified port number. | |
The client must be aware of these parameters | |
""" | |
server.bind((IP_address, Port)) | |
server.listen(1) | |
print "%s Serving on port %s ..." % (IP_address, Port) | |
resp = "Wrong" | |
while True: | |
conn, addr = server.accept() | |
print('Connected by', addr) | |
conn.settimeout(10) | |
while True: | |
try: | |
req = conn.recv(2048) | |
if req == "Ping": | |
resp = "Pong" | |
conn.send(resp) | |
except socket.timeout: | |
pass | |
if not req: | |
print 'Connection lost. Listening for a new controller.' | |
break | |
#Timeout occurred, do things | |
conn.close() | |
if __name__ == '__main__': | |
serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment