mininet-sample/utils/msg_client.py

73 lines
2.2 KiB
Python
Raw Permalink Normal View History

import argparse
import datetime
import random
import socket
import string
import time
BUFFER_SIZE = 2048
ENCODING = 'ascii'
threads = {}
to_join = []
parser = argparse.ArgumentParser(description="Msg client")
parser.add_argument("-s", "--sleep", type=float, help="sleep time between two sendings", default=5.0)
parser.add_argument("-n", "--nb", type=int, help="number of requests done", default=5)
2017-02-23 11:21:04 +00:00
parser.add_argument("-B", "--bulk", help="if set, don't wait for reply to send another packet", action="store_true")
2017-03-01 15:20:38 +00:00
parser.add_argument("-b", "--bytes", type=int, help="number of bytes to send and receive", default=1200)
args = parser.parse_args()
def string_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Handle reusing the same 5-tuple if the previous one is still in TIME_WAIT
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
2017-02-23 13:49:44 +00:00
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CORK, 0)
# Bind the socket to the port
server_address = ('10.1.0.1', 8000)
print("Try to connect to %s port %s" % server_address)
sock.connect(server_address)
delays = []
try:
for i in range(args.nb):
time.sleep(args.sleep)
2017-02-28 20:08:58 +00:00
request = string_generator(size=args.bytes, chars=string.digits)
start_time = datetime.datetime.now()
sock.sendall(request.encode(ENCODING))
if args.bulk:
continue
buffer_data = ""
2017-02-28 20:08:58 +00:00
while len(buffer_data) < args.bytes:
data = sock.recv(BUFFER_SIZE).decode(ENCODING)
if len(data) == 0:
# Connection closed at remote; close the connection
break
buffer_data += data
2017-02-28 20:08:58 +00:00
if len(buffer_data) == args.bytes:
stop_time = datetime.datetime.now()
delays.append(stop_time - start_time)
else:
print("An issue occured...")
break
finally:
# Clean up the connection
print("Closing connection")
sock.close()
2017-02-24 15:12:50 +00:00
for delay in delays:
2017-02-27 08:19:45 +00:00
print(delay.total_seconds())