python - cherrypy didn't work correctly whith daemonize -
i'm new in cherrypy, , try develop small application using framework. problem is, application can serves , browser can access when cherrypy works in undaemonizer mode. when write code using cherrypy.process.plugins.daemonizer(), cherrypy started in background, , listen on specific port, browser connection refused( iptables or ufw shutted down still inaccessible ). incredible thing is, when start daemoned mode, can still start undaemoned process , , listen on same port. wonder why happend, , how solve it?
simply saids: daemonizer starts, cherrypy listened on specified port, browser connection refused; without daemonizer, cherrypy works well.
thanks alot
with code
from optparse import optionparser cherrypy.process.plugins import daemonizer cherrypy.process.plugins import pidfile import cherrypy import json import urllib import datetime try: import cpickle pickle except: import pickle import time import base64 import os import sys ''' cherrypy class ''' class index(object): @cherrypy.expose def index(self): return "say hello yellow elephant" class system(object): @cherrypy.expose def env(self, token): local_token = token() if local_token.authtoken(token) true: env = get_env() return json.dumps(env) return '{"errcode", "invalid token"}' class jmx(object): @cherrypy.expose def get(self, token, host, port, qry): local_token = token() if local_token.authtoken(token) true: url = 'http://' + host + ':' + port + '/jmx?qry=' + qry jmx = urllib.urlopen(url) jmx_data = jmx.read().replace('\n', '') jmx.close() return jmx_data return '{"errcode", "invalid token"}' """ command uses base64 encode using http post method """ class command(object): def __init__(self): self.fname = datetime.datetime.now().strftime('%y-%m-%d_%m-%m-%s') + '.log' @cherrypy.expose def run(self, token, command): local_token = token() command = base64.b64decode(command) if local_token.authtoken(token) true: os.popen(command + ' 2>&1 > /usr/lib/agent/output/' + self.fname) return '{"errcode", "invalid token"}' @cherrypy.expose def readlog(self, token): local_token = token() if local_token.authtoken(token) true: log = open('/usr/lib/agent/output/' + self.fname) lines = log.readlines() log.close() return json.dumps(lines, ensure_ascii=false) return '{"errcode", "invalid token"}' """ first time access central, create new token on slave node, token pickle.dump(cacl_mysql_passwd(conf['agent']['secret'])) token created , if central makes change secret, slave node inaccessible!!! """ class token(object): def authtoken(self, token): if(os.path.isfile('/usr/lib/agent/key/authenticate.key')) false: return self.createtoken(token) else: try: k = open('/usr/lib/agent/key/authenticate.key', 'rb') tokenizer = pickle.load(k) k.close() if token == tokenizer: return true else: return false except ioerror, e: return '{"errcode":"' + str(e).replace('\n', '<br/>') + '"}' @cherrypy.expose def createtoken(self, token): if(os.path.isfile('/usr/lib/agent/key/authenticate.key')) false: try: k = open('/usr/lib/agent/key/authenticate.key', 'wb') pickle.dump(token, k) k.close() return true except ioerror, e: return '{"exception":"' + str(e).replace('\n', '<br/>') + '"}' else: return '{"errcode":"token exists"}' class controller: def __init__(self, pidfile='/var/run/agent/agent.pid', host='0.0.0.0', port=30050): self.port = port self.host = host self.pidfile = pidfile self.settings = { 'global': { 'server.socket_port': port, 'server.socket_host': host, 'server.socket_file': '', 'server.socket_queue_size': 5, 'server.protocol_version': 'http/1.1', 'server.log_to_screen': true, 'server.log_file': '', 'server.reverse_dns': false, 'server.thread_pool': 10, 'server.environment': 'production', 'engine.timeout_monitor.on': false } } def start(self): if os.path.exists(self.pidfile): sys.stderr.write('pid file exists, server running?\n') sys.exit(1) else: daemonizer(cherrypy.engine, stdin='/dev/stdin', stdout='/dev/stdout', stderr='/dev/stderr').subscribe() pidfile(cherrypy.engine, self.pidfile).subscribe() cherrypy.tree.mount(index(), '/') cherrypy.tree.mount(system(), '/system') cherrypy.tree.mount(command(), '/command') cherrypy.tree.mount(jmx(), '/jmx') cherrypy.config.update(self.settings) cherrypy.engine.start() cherrypy.engine.block() def stop(self): cherrypy.config.update(self.settings) if os.path.exists(self.pidfile): cherrypy.engine.stop() cherrypy.engine.exit() try: process = open(self.pidfile).read().strip() if process != 0: os.popen('kill -9 %s' % process) os.remove(self.pidfile) except ioerror, e: sys.stderr.write(str(e)) else: sys.stderr.write('pid file not exist, server gone?\n') sys.exit(1) if '__main__' == __name__: cherrypy.engine.autoreload.stop() cherrypy.engine.autoreload.unsubscribe() syntax = 'syntax: %prog -b 192.168.1.1 -s start' parser = optionparser(usage=syntax) ip = os.popen('hostname -i').read().strip() hostname = os.popen('hostname --fqdn').read().strip() parser.add_option('-b', '--bind', action='store', type='string', dest='bind', default=ip, help='inner network ip address, default value hostname -i') parser.add_option('-s', '--signal', action='store', type='string', dest='signal', help='valid signal {start|stop|restart}') options, args = parser.parse_args() if len(sys.argv) == 1: print 'use %s -h or --help help.' % sys.argv[0] else: if options.signal == '': print 'must give -s option\'s value' else: daemon = controller(pidfile='/var/run/agent/agent.pid', host=options.bind) if 'start' == options.signal: daemon.start() elif 'stop' == options.signal: daemon.stop() else: print 'invalid signal' sys.exit(1)
Comments
Post a Comment