def wait_network_port_idle(port):
    """Wait network port idle.

    return after 5 minutes or network port idle.
    TCP connect close TIME_WAIT max 2MSL(2minutes)

    :param port: network port
    """
    for i in range(0, 30):  # 5 minutes
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind(('0.0.0.0', port))
            sock.close()        # close effect immediately if not data send
            return
        except __HOLE__:
            if i == 0:
                time.sleep(1)   # sometime just need short wait
            else:
                log.info('closing port [%d], Please wait a moment.', port)
                time.sleep(10)
    # import gc
    # from werkzeug.serving import BaseWSGIServer
    # for o in gc.get_referrers(BaseWSGIServer):
    #     if o.__class__ is BaseWSGIServer:
    #         o.server_close()
dataset/ETHPy150Open remyzane/flask-http-api/http_api/assist/coding.py/wait_network_port_idle