コード例 #1
0
ファイル: ajaxterm.py プロジェクト: easyVDR/five
def main():
	parser = optparse.OptionParser()
	parser.add_option("-p", "--port", dest="port", default="8022", help="Set the TCP port (default: 8022)")
	parser.add_option("-c", "--command", dest="cmd", default=None,help="set the command (default: /bin/login or ssh localhost)")
	parser.add_option("-l", "--log", action="store_true", dest="log",default=0,help="log requests to stderr (default: quiet mode)")
	parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=0, help="run as daemon in the background")
	parser.add_option("-P", "--pidfile",dest="pidfile",default="/var/run/ajaxterm.pid",help="set the pidfile (default: /var/run/ajaxterm.pid)")
	parser.add_option("-i", "--index", dest="index_file", default="ajaxterm.html",help="default index file (default: ajaxterm.html)")
	parser.add_option("-u", "--uid", dest="uid", help="Set the daemon's user id")
	parser.add_option("-s", "--serverport", dest="serverport", help="Use a different port than 22 to connect to the ssh server")
	parser.add_option("-t", "--token", dest="token", help="Set authorization token")
	parser.add_option("-T", "--terminate-on-idle", metavar="SEC", action="store", type="int", dest="term_on_idle", help="Terminate if idle for more than SEC seconds (0 means never)")
	(o, a) = parser.parse_args()
	if o.daemon:
		pid=os.fork()
		if pid == 0:
			#os.setsid() ?
			os.setpgrp()
			nullin = file('/dev/null', 'r')
			nullout = file('/dev/null', 'w')
			os.dup2(nullin.fileno(), sys.stdin.fileno())
			os.dup2(nullout.fileno(), sys.stdout.fileno())
			os.dup2(nullout.fileno(), sys.stderr.fileno())
			if os.getuid()==0 and o.uid:
				try:
					os.setuid(int(o.uid))
				except:
					os.setuid(pwd.getpwnam(o.uid).pw_uid)
		else:
			try:
				file(o.pidfile,'w+').write(str(pid)+'\n')
			except:
				pass
			print 'AjaxTerm at http://localhost:%s/ pid: %d' % (o.port,pid)
			sys.exit(0)
	else:
		print 'AjaxTerm at http://localhost:%s/' % o.port
	at=AjaxTerm(o.cmd,o.index_file,o.serverport,o.token,o.term_on_idle)
#	f=lambda:os.system('firefox http://localhost:%s/&'%o.port)
#	qweb.qweb_wsgi_autorun(at,ip='localhost',port=int(o.port),threaded=0,log=o.log,callback_ready=None)
	try:
		global g_server
		g_server = qweb.QWebWSGIServer(at,ip='localhost',port=int(o.port),threaded=1,log=o.log)
		g_server.serve_forever()
	except KeyboardInterrupt,e:
		sys.excepthook(*sys.exc_info())
コード例 #2
0
def main():
    parser = optparse.OptionParser()
    parser.add_option("-p",
                      "--port",
                      dest="port",
                      default="8022",
                      help="Set the TCP port (default: 8022)")
    parser.add_option(
        "-c",
        "--command",
        dest="cmd",
        default=None,
        help="set the command (default: /bin/login or ssh localhost)")
    parser.add_option("-l",
                      "--log",
                      action="store_true",
                      dest="log",
                      default=0,
                      help="log requests to stderr (default: quiet mode)")
    parser.add_option("-d",
                      "--daemon",
                      action="store_true",
                      dest="daemon",
                      default=0,
                      help="run as daemon in the background")
    parser.add_option("-P",
                      "--pidfile",
                      dest="pidfile",
                      default="/var/run/ajaxterm.pid",
                      help="set the pidfile (default: /var/run/ajaxterm.pid)")
    parser.add_option("-i",
                      "--index",
                      dest="index_file",
                      default="ajaxterm.html",
                      help="default index file (default: ajaxterm.html)")
    parser.add_option("-u",
                      "--uid",
                      dest="uid",
                      help="Set the daemon's user id")
    (o, a) = parser.parse_args()
    if o.daemon:
        pid = os.fork()
        if pid == 0:
            #os.setsid() ?
            os.setpgrp()
            nullin = open('/dev/null', 'r')
            nullout = open('/dev/null', 'w')
            os.dup2(nullin.fileno(), sys.stdin.fileno())
            os.dup2(nullout.fileno(), sys.stdout.fileno())
            os.dup2(nullout.fileno(), sys.stderr.fileno())
            if os.getuid() == 0 and o.uid:
                try:
                    os.setuid(int(o.uid))
                except:
                    os.setuid(pwd.getpwnam(o.uid).pw_uid)
        else:
            try:
                open(o.pidfile, 'w+').write(str(pid) + '\n')
            except:
                pass
            print('AjaxTerm at http://localhost:{0}/ pid: {1}'.format(
                o.port, pid))
            sys.exit(0)
    else:
        print('AjaxTerm at http://localhost:{0}/'.format(o.port))
    at = AjaxTerm(o.cmd, o.index_file)
    #	f=lambda:os.system('firefox http://localhost:%s/&'%o.port)
    #	qweb.qweb_wsgi_autorun(at,ip='localhost',port=int(o.port),threaded=0,log=o.log,callback_ready=None)
    try:
        qweb.QWebWSGIServer(at,
                            ip='localhost',
                            port=int(o.port),
                            threaded=0,
                            log=o.log).serve_forever()
    except KeyboardInterrupt as e:
        sys.excepthook(*sys.exc_info())
    at.multi.die()