def wait_network_port_idle(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 OSError:
            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()
Wait network port idle.
dataset/ETHPy150Open remyzane/flask-http-api/http_api/assist/coding.py/wait_network_port_idle