class_docstr: The ZMQ socket object
  
  To create a Socket, first create a Context::
  
      ctx = zmq.Context.instance()
  
  then call ``ctx.socket(socket_type)``::
  
      s = ctx.socket(zmq.ROUTER)
class_docstr_tok: ["The","ZMQ","socket","object","To","create","a","Socket",",","first","create","a","Context",":",":","ctx","=","zmq.Context.instance","(",")","then","call","``","ctx.socket","(","socket_type",")","``",":",":","s","=","ctx.socket","(","zmq.ROUTER",")"]
docstr: Bind this socket to a random port in a range.
  
  If the port range is unspecified, the system will choose the port.
  
  Parameters
  ----------
  addr : str
      The address string without the port to pass to ``Socket.bind()``.
  min_port : int, optional
      The minimum port in the range of ports to try (inclusive).
  max_port : int, optional
      The maximum port in the range of ports to try (exclusive).
  max_tries : int, optional
      The maximum number of bind attempts to make.
  
  Returns
  -------
  port : int
      The port the socket was bound to.
  
  Raises
  ------
  ZMQBindError
      if `max_tries` reached before successful bind
docstr_tok: ["Bind","this","socket","to","a","random","port","in","a","range",".","If","the","port","range","is","unspecified",",","the","system","will","choose","the","port",".","Parameters","--","--","--","--","--","addr",":","str","The","address","string","without","the","port","to","pass","to","``","Socket.bind","(",")","``",".","min_port",":","int",",","optional","The","minimum","port","in","the","range","of","ports","to","try","(","inclusive",")",".","max_port",":","int",",","optional","The","maximum","port","in","the","range","of","ports","to","try","(","exclusive",")",".","max_tries",":","int",",","optional","The","maximum","number","of","bind","attempts","to","make",".","Returns","--","--","--","-","port",":","int","The","port","the","socket","was","bound","to",".","Raises","--","--","--","ZMQBindError","if","`","max_tries","`","reached","before","successful","bind"]
code: def bind_to_random_port(
      self, addr, min_port=49152, max_port=65536, max_tries=100
  ):
      """Bind this socket to a random port in a range.
  
      If the port range is unspecified, the system will choose the port.
  
      Parameters
      ----------
      addr : str
          The address string without the port to pass to ``Socket.bind()``.
      min_port : int, optional
          The minimum port in the range of ports to try (inclusive).
      max_port : int, optional
          The maximum port in the range of ports to try (exclusive).
      max_tries : int, optional
          The maximum number of bind attempts to make.
  
      Returns
      -------
      port : int
          The port the socket was bound to.
  
      Raises
      ------
      ZMQBindError
          if `max_tries` reached before successful bind
      """
      if (
          hasattr(constants, "LAST_ENDPOINT")
          and min_port == 49152
          and max_port == 65536
      ):
          # if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
          # we can bind to port 0 and let the OS do the work
          self.bind("%s:*" % addr)
          url = self.last_endpoint.decode("ascii", "replace")
          _, port_s = url.rsplit(":", 1)
          return int(port_s)
  
      for i in range(max_tries):
          try:
              port = random.randrange(min_port, max_port)
              self.bind("%s:%s" % (addr, port))
          except ZMQError as exception:
              en = exception.errno
              if en == zmq.EADDRINUSE:
                  continue
              elif sys.platform == "win32" and en == errno.EACCES:
                  continue
              else:
                  raise
          else:
              return port
      raise ZMQBindError("Could not bind socket to random port.")
  
imports: import errno
  import random
  import sys
  import warnings
  import zmq
  from zmq.backend import Socket
  from .poll import Poller
  from .None import constants
  from .attrsettr import AttributeSetter
  from zmq.error import ZMQError, ZMQBindError
  from zmq.utils import jsonapi
  from zmq.utils.strtypes import bytes, unicode, basestring
  from .constants import SNDMORE, ENOTSUP, POLLIN, int64_sockopt_names, int_sockopt_names, bytes_sockopt_names, fd_sockopt_names
label_desc: Use image node_id 16 for calling the Socket obj's underlying member method code with example usage: obj.bind_to_random_port(addr, min_port, max_port, max_tries) and returns: int, port