def handle(self): myenv = Gtenv("") if myenv.istrial: rand = random.randint(0,100) if rand == 90: licdays,licdate = trial.haskey() if licdays == 0 or (licdate - date.today()).days < 0: self.server.logger.warning('试用授权到期或没有正式授权文件,系统启动失败!') myenv.running = False return else: rand = random.randint(0,1000) if rand == 90: licdays,licdate = auth.haslic() if licdays == 0 or (licdate - date.today()).days < 0: self.server.logger.warning('试用授权到期或没有正式授权文件,系统启动失败!') myenv.running = False return (ip, port) = self.client_address self.server.logger.log (logging.INFO, "Request from '%s'", ip) if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients: self.raw_requestline = self.rfile.readline() if self.parse_request(): self.send_error(403) else: try:self.__base_handle() except socket.error, arg: try: msg = arg[1] except: msg = arg self.send_error(404, msg) self.server.logger.log(logging.ERROR,'Exception happened during processing'+\ ',error info:'+msg)
def startweb(): #parse_command_line is very important tornado.options.parse_command_line() #print tornado.options.options.as_dict() path = sys.path[0] if os.path.isfile(path): path = os.path.dirname(path) #print path gtdir = path webenv = Gtenv(path) path = webenv.getpath() logfile = '' try: cf = ConfigParser.ConfigParser() cf.read(path + "/service.ini") logfile = path + "/" + cf.get("log","local") except Exception as e: pass if logfile == '': logfile = path + "/" +DEFAULT_LOG_FILENAME daemon = False max_log_size = 2 logger = logSetup (logfile, max_log_size, daemon) writepac(True) licdays,licdate = trial.haskey() if licdays == 0 or (licdate - date.today()).days < 0: licdays,licdate = auth.haslic() if licdays == 0 or (licdate - date.today()).days < 0: writepac(False) logger.warning(u'试用授权到期或没有正式授权文件,系统启动失败!') return 0 else: webenv.istrial = False webenv.licdate = licdate else: webenv.istrial = True webenv.licdate = licdate writelicjs() settings = { "static_path": path + "/static", } app = tornado.web.Application([ (r"/", Hello), (r"/hello", Hello), (r"/getlic", Lic), (r"/repost", Repost), (r"/job", Job), (r"/testjob", ThreadJob), (r"/querynsr", Querynsr), (r"/querytrace", Querytrace), (r"/download",Downloader), (r"/importnsr",ImoportNsr), (r"/rquery",RemoteQuery), (r"/sysinfo",G3info), (r"/SaveRemoteQuery",SaveRemoteQuery), (r"/jsonp",JSONP), (r"/reget",Reget), (r"/czy",CZY), (r"/queryfreq",QueryFreq), (r"/savefreq",SaveFreq), (r"/nsrlistjsonp",NsrlistJsonp), (r"/nsrinfo",GetNsrDetail) ],**settings) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(8001) logger.warning("Servering HTTP on localhost port:%s",8001) #signal never work in windows system #signal.signal(signal.SIGTERM, sig_handler) #signal.signal(signal.SIGINT, sig_handler) try: tornado.ioloop.IOLoop.instance().start() #KeyboardInterrupt never work #except KeyboardInterrupt: # logger.log(logging.INFO,"KeyboardInterrupt") # shutdown() except Exception as e: print e logger.warning("exception")
return 1 for opt, value in opts: if opt == "-c": cfgfile = value if opt == "-p": port = int (value) if opt == "-l": logfile = value if opt == "-d": daemon = not daemon if opt == "-h": usage () return 0 licdays,licdate = trial.haskey() if licdays == 0 or (licdate - date.today()).days < 0: licdays,licdate = auth.haslic() if licdays == 0 or (licdate - date.today()).days < 0: logger.info('试用授权到期或没有正式授权文件,系统启动失败!') return 0 #if daemon: # pass #daemonize (logger) signal.signal (signal.SIGINT, handler) if args: allowed = [] for name in args: client = socket.gethostbyname(name) allowed.append(client) logger.log (logging.INFO, "Accept: %s (%s)" % (client, name))
def main (): path = sys.path[0] if os.path.isfile(path): path = os.path.dirname(path) #path = "F:\\release" gtdir = path myenv = Gtenv(path) path = myenv.getpath() cfgfile = path + '/config.json' logfile = "" try: cf = ConfigParser.ConfigParser() cf.read(path + "/service.ini") logfile = path + "/" + cf.get("log","proxy") except Exception as e: pass if logfile == '': logfile = path + '/proxy.log' daemon = False max_log_size = 2 port = 8000 allowed = [] run_event = threading.Event () local_hostname = socket.gethostname () ''' try: opts, args = getopt.getopt (sys.argv[1:], "l:dhp:", []) except getopt.GetoptError, e: usage (str (e)) return 1 for opt, value in opts: if opt == "-c": cfgfile = value if opt == "-p": port = int (value) if opt == "-l": logfile = value if opt == "-d": daemon = not daemon if opt == "-h": usage () return 0 ''' # setup the log file logger = logSetup (logfile, max_log_size, daemon) #logger.info("a..........") licdays,licdate = trial.haskey() if licdays == 0 or (licdate - date.today()).days < 0: licdays,licdate = auth.haslic() if licdays == 0 or (licdate - date.today()).days < 0: logger.warning('试用授权到期或没有正式授权文件,系统启动失败!') return 0 else: myenv.istrial = False myenv.licdate = licdate else: myenv.istrial = True myenv.licdate = licdate if daemon: pass #daemonize (logger) #signal.signal (signal.SIGINT, handler) #必须屏蔽 #logger.info("b..........") ''' if args: allowed = [] for name in args: client = socket.gethostbyname(name) allowed.append(client) logger.log (logging.INFO, "Accept: %s (%s)" % (client, name)) ProxyHandler.allowed_clients = allowed else: logger.log (logging.WARNING, "Any clients will be served...") ''' cfgcls = ProxyConfig.config(cfgfile) cfg = {} (cfg['Redirect'],cfg['Modify'],cfg['Repost']) = cfgcls.read() ProxyHandler.config = cfg rulematch = RuleMatch.RuleMatch(cfg['Redirect'],cfg['Modify'],cfg['Repost']) ProxyHandler.rulehandler = rulematch #server_address = (socket.gethostbyname (local_hostname), port) server_address = ('127.0.0.1', port) ProxyHandler.protocol = "HTTP/1.0" httpd = ThreadingHTTPServer (server_address, ProxyHandler, logger) sa = httpd.socket.getsockname () logger.warning("Servering HTTP Proxy on %s port %s", sa[0], sa[1]) req_count = 0 myenv.running = True myenv.checked = False while myenv.running and not run_event.isSet (): try: httpd.handle_request () req_count += 1 if req_count == 1000: logger.log (logging.INFO, "Number of active threads: %s", threading.activeCount ()) req_count = 0 if not myenv.running: httpd.__is_shut_down = True except select.error, e: if e[0] == 4: pass else: logger.log (logging.CRITICAL, "Errno: %d - %s", e[0], e[1])