def check_random_port(port, socket_type='all'):
 assert (socket_type in ('all', 'tcp', 'udp')), ('Invalid socket type %s' % type(socket_type))
 assert isinstance(port, int), ('Invalid port type %s' % type(port))
 assert (0 < port <= 65535), ('Invalid port value %s' % port)
 _family = socket.AF_INET
 _sock_type = None
 if (socket_type == 'udp'):
  _sock_type = socket.SOCK_DGRAM
 elif (socket_type == 'tcp'):
  _sock_type = socket.SOCK_STREAM
 is_port_working = False
 if (socket_type == 'all'):
  if _test_port(_family, socket.SOCK_DGRAM, port):
   is_port_working = _test_port(_family, socket.SOCK_STREAM, port)
 else:
  is_port_working = _test_port(_family, _sock_type, port)
 return is_port_working

def check_random_port ( port , socket_type = 'all' ) : assert ( socket_type in ( 'all' , 'tcp' , 'udp' ) ) , ( 'Invalid socket type %s' % type ( socket_type ) ) assert isinstance ( port , int ) , ( 'Invalid port type %s' % type ( port ) ) assert ( 0 < port <= 65535 ) , ( 'Invalid port value %s' % port ) _family = socket . AF_INET _sock_type = None if ( socket_type == 'udp' ) : _sock_type = socket . SOCK_DGRAM elif ( socket_type == 'tcp' ) : _sock_type = socket . SOCK_STREAM is_port_working = False if ( socket_type == 'all' ) : if _test_port ( _family , socket . SOCK_DGRAM , port ) : is_port_working = _test_port ( _family , socket . SOCK_STREAM , port ) else : is_port_working = _test_port ( _family , _sock_type , port ) return is_port_working
returns an usable port number that can be bound with by the specific type of socket .