title: How do I get free ports in the specific range in python
body: <p>I can use python to get free ports, but I want to get a specific range of free ports. </p>
  
  <p>I try to get it using a while loop, but I can't get the vaule I wanted, maybe take a long time</p>
  
  <pre><code>import socket
  
  SO_BINDTODEVICE=25
  
  def get_free_port(iface=None):
      s = socket.socket()
  
      if iface:
          s.setsockopt(socket.SOL_SOCKET, SO_BINDTODEVICE, bytes(iface,'utf8'))
  
      s.bind(('',0))
      port = 0
      while(port&lt;60100 or port&gt;60300):
          ip = s.getsockname()[0]
          port = s.getsockname()[1]
      s.close()
  
      return ip,port
  
  
  print(get_free_port())
  </code></pre>
  
  <p>I hope to get the port I want in a short time</p>