def run_server(settings):
    settings['state'] = {'num_gets': 0, 'num_posts': 0, 'num_posts_in_progress': 0}
    curlbomb_args = dict(
        resource=settings['resource'],
        state=settings['state'],
        allowed_gets=settings['num_gets'],
        knock=settings['knock'],
        mime_type=settings['mime_type'],
        allow_post_backs=settings['receive_postbacks'],
        log_post_backs=settings['log_post_backs'],
        log_file=settings['log_file'],
        get_callback=settings.get('get_callback', None)
    )
    
    unwrapped_script = settings['get_curlbomb_command'](settings, unwrapped=True)
    if not settings['client_quiet'] and settings['time_command']:
        unwrapped_script = "time "+unwrapped_script

    app = tornado.web.Application(
        [
            (r"/", CurlbombResourceWrapperRequestHandler,
             dict(curlbomb_command=unwrapped_script)),
            (r"/r", CurlbombResourceRequestHandler, curlbomb_args),
            (r"/s", CurlbombStreamRequestHandler, curlbomb_args)
        ], default_handler_class=ErrorRequestHandler
    )

    global httpd
    httpd = app.listen(
        settings['port'],
        ssl_options=settings['ssl_context'],
        max_buffer_size=1024E9)

    ## Start SSH tunnel if requested:
    httpd.ssh_conn = None
    if settings['ssh']:
        if settings['ssl'] is False:
            log.warn("Using --ssh without --ssl is probably not a great idea")
        httpd.ssh_conn = SSHRemoteForward(
            settings['ssh_host'], settings['ssh_forward'], settings['ssh_port'])
        httpd.ssh_conn.start()
        if not httpd.ssh_conn.wait_connected():
            log.error(httpd.ssh_conn.last_msg)
            sys.exit(1)

    cmd = settings['get_curlbomb_command'](settings)
    if not settings['quiet']:
        if settings['stdout'].isatty():
            sys.stderr.write("Paste this command on the client:\n")
            sys.stderr.write("\n")
            sys.stderr.write("  {}\n".format(cmd))
            sys.stderr.write("\n")
        else:
            sys.stderr.write("{}\n".format(cmd))

    # Disable port forward checker for now. Good idea, but it doesn't work reliably.
    # 
    # if settings['ssh'] and not settings['args'].domain:
    #     "Check the SSH forward works"
    #     def check_port_forward(timeout=5):
    #         time.sleep(5)
    #         try:
    #             url = "http{ssl}://{host}:{port}".format(
    #                 ssl="s" if settings['ssl'] is not False else "",
    #                 host=settings['display_host'],
    #                 port=settings['display_port'])
    #             log.info("Testing port forward is functioning properly - {}".format(url))
    #             r = requests.head(url, timeout=timeout)
    #         except (requests.ConnectionError, requests.exceptions.ReadTimeout):
    #             log.warn("Could not contact server throuh SSH forward. You may need to check your sshd_config and enable 'GatwayPorts clientspecified'")
    #     _thread.start_new_thread(check_port_forward, ())

    try:
        log.debug("server ready")
        tornado.ioloop.IOLoop.current().start()
    except __HOLE__:
        if settings['verbose']:
            traceback.print_exc()
    finally:
        httpd.stop()
        if httpd.ssh_conn is not None:
            httpd.ssh_conn.kill()
        settings['resource'].close()
        if settings['log_process']:
            settings['log_file'].close()
            settings['log_process'].wait()
            log.info("run_server done")

    return settings.get('return_code', 0)
dataset/ETHPy150Open EnigmaCurry/curlbomb/curlbomb/server.py/run_server