X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fcommon%2Futils.py;h=92bb7b7d35c2e87af61f1020af831020df2f87b7;hb=c1a4f5028f88e7e97e3ae2cc3f8aae6bf67c07b2;hp=a4f7b30dca22ea0bd260292cf4f95f09e854aa3d;hpb=2945003ca74fd3d73621c9e1745dd1662b88c2df;p=yardstick.git diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index a4f7b30dc..92bb7b7d3 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -24,7 +24,10 @@ import os import subprocess import sys import collections +import socket +import random from functools import reduce +from contextlib import closing import yaml import six @@ -263,3 +266,11 @@ def set_dict_value(dic, keys, value): else: return_dic = return_dic[key] return dic + + +def get_free_port(ip): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + while True: + port = random.randint(5000, 10000) + if s.connect_ex((ip, port)) != 0: + return port