def main(host='localhost', port=6000, authkey='secret password'):
    "Debug a script and accept a remote frontend"

    if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"):
        print "usage: pdb.py scriptfile [arg] ..."
        sys.exit(2)

    mainpyfile = sys.argv[1]     # Get script filename
    if not os.path.exists(mainpyfile):
        print 'Error:', mainpyfile, 'does not exist'
        sys.exit(1)

    del sys.argv[0]         # Hide "pdb.py" from argument list

    # Replace pdb's dir with script's dir in front of module search path.
    sys.path[0] = os.path.dirname(mainpyfile)

    from multiprocessing.connection import Listener
    address = (host, port)     # family is deduced to be 'AF_INET'
    listener = Listener(address, authkey=authkey)
    print "qdb debugger backend: waiting for connection at", address
    conn = listener.accept()
    print 'qdb debugger backend: connected to', listener.last_accepted

    # create the backend
    qdb = Qdb(conn, redirect_stdio=True, allow_interruptions=True)
    try:
        print "running", mainpyfile
        qdb._runscript(mainpyfile)
        print "The program finished"
    except SystemExit:
        # In most cases SystemExit does not warrant a post-mortem session.
        print "The program exited via sys.exit(). Exit status: ",
        print sys.exc_info()[1]
        raise
    except:
        raise

    conn.close()
    listener.close()
dataset/ETHPy150Open uwdata/termite-data-server/web2py/gluon/contrib/qdb.py/main/original