Пример #1
0
    def handle(self,client):
        """
        """
        
        try:
            #pdb.set_trace()
            new_sock ,addr = client
            res = new_sock.recv(2048)
            path=""

            try:        
                html = HttpParse(res)
                path = html._Header['Request']
                if html._Header['Method']=='POST':
                    body_len = int(html._Header['Content-Length'])
                    if len(html._DATA) < body_len:
                        while 1:
                            c = new_sock.recv(512)
                            html._DATA = html._DATA + c
                            if len(html._DATA) >= body_len:
                                break

                    if 'Content-Type' in html._Header and html._Header['Content-Type'].find('multipart/form-data') >-1:
                        html._parse_multipart()
                    else:
                        html._parse_post()    
            except Exception as e:
                raise Exception('The request of the error')
            #取得path前两级赋值予 模块名,函数名
            mod, func = html.getpath()[:2]
            kw  = html._GET or html._POST or html.Up_File or html._DATA or {}
            if mod == 'cmd': 
                if func == 'reload_tasks':
                    reload_tasks()
                elif func == 'reload_cmd':
                    reload_cmd()
                else:
                    models = dir(commands_)
                    if func in models:
                        f = eval('commands_.%s' % func)
                        rs = json_dumps(f(kw))
                logger.info('"%s" "%s" "cmd"' % (addr[0],path) )
            else:             
                try:
                    exec("from %s import %s" % (mod, func))
                    f = eval(func)
                    rs = json.dumps(f(kw))
                    
                except Exception as e:
                    #raise e
                    traceback.print_exc()

            logger.info('"%s" "%s"' % (addr[0],path) )
            h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
            
            if rs:
                h = 'HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s' % (len(rs),rs)

            new_sock.send(h.encode())
            new_sock.close()
            
            self._report += 1
        except Exception as e:
            traceback.print_exc()
            errinfo = traceback.format_exc()
            #errinfo='出错了'
            #logger.error('"%s" "%s" "erron" "%s"' % (addr[0],path,errinfo))
            req = 'HTTP/1.1 600 system error!\r\nContent-Length: %d\r\n\r\n%s' % (len(str(e)),str(e))
            #req = 'HTTP/1.1 500 system error!\r\n\r\n'
            new_sock.send(req.encode())
            new_sock.close()
Пример #2
0
    def handle(self,client):
        """
        """
        t=float('%0.3f'%time.time())
        try:
            #pdb.set_trace()
            new_sock ,addr = client
            res = new_sock.recv(2048)
            path=""
            try:        
                html = HttpParse(res)
                path = html._Header['Request']
                if html._Header['Method']=='POST':
                    body_len = int(html._Header['content-length'])
                    if len(html._DATA) < body_len:
                        while 1:
                            c = new_sock.recv(512)
                            html._DATA = html._DATA + c
                            if len(html._DATA) >= body_len:
                                break

                    if 'content-type' in html._Header and html._Header['content-type'].find('multipart/form-data') >-1:
                        html._parse_multipart()
                    else:
                        html._parse_post()    
            except Exception as e:
                traceback.print_exc()
                raise Exception('The request of the error')
            #路由解析
            try:
                mod, func = html.getpath()[:2]
            except:
                h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
                new_sock.send(h.encode())
                new_sock.close()
                return
                   
            kw  = html._GET or html._POST or html.Up_File or html._DATA or {}
            #print(kw)
            rs = ""
            if mod == 'cmd': 
                if func == 'reload_tasks':
                    reload_tasks()
                elif func == 'reload_cmd':
                    reload_cmd()
                else:
                    models = dir(commands_)
                    if func in models:
                        f = eval('commands_.%s' % func)
                        rs = json_dumps(f(kw))
                logger.info('"%s" "%s" "cmd"' % (addr[0],path) )
            else:             
                try:
                    if type(kw) != dict:
                        rs = "[-102,'data body not json format.'"
                    else:
                        exec("from ctl_%s import %s" % (mod, func))
                        f = eval(func)
                        #rs = json.dumps(f(kw))
                        rs = JSONEncoder().encode(f(kw))
                    
                except Exception as e:
                    #raise e
                    traceback.print_exc()
                    rs = "[-101,'sys erro']"
            ftime = float('%0.3f'%(time.time()-t))
            logger.info('"%s" "%s" "%s"' % (addr[0],path,ftime) )
            h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
            
            if rs:
                h = 'HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s' % (len(rs),rs)

            new_sock.send(h.encode())
            new_sock.close()
            
            self._report += 1
        except Exception as e:
            traceback.print_exc()
            errinfo = traceback.format_exc()
            #errinfo='出错了'
            #logger.error('"%s" "%s" "erron" "%s"' % (addr[0],path,errinfo))
            req = 'HTTP/1.1 500 system error!\r\nContent-Length: %d\r\n\r\n%s' % (len(str(e)),str(e))
            #req = 'HTTP/1.1 500 system error!\r\n\r\n'
            new_sock.send(req.encode())
            new_sock.close()
Пример #3
0
    def handle(self,client):
        """
        """
        
        try:
            #pdb.set_trace()
            new_sock ,addr = client
            res = new_sock.recv(2048)
            path=""

            try:        
                html = HttpParse(res)
                if html._Header['Method']=='POST':
                    print(html._Header)
                    body_len = int(html._Header['content-length'])
                    if len(html._DATA) < body_len:
                        while 1:
                            c = new_sock.recv(512)
                            html._DATA = html._DATA + c
                            if len(html._DATA) >= body_len:
                                break
                        if html._Header['Content-Type'].index('multipart/form-data') >-1:
                            html._parse_multipart()
                        else:
                            html._parse_post()
            except Exception as e:
                raise Exception('The request of the error')
            path = html._Header['Request']
            #action, func = path.split('/')[:2]
            query = html.getpath()
            action, func = query[:2]
            
            args = ()
            if action == 'cmd': 
                kw  = html._GET or html._POST or json_loads(html._DATA) or {}

                if func == 'reload_tasks':
                    reload_tasks()
                    rs = "OK"
                elif func == 'reload_cmd':
                    reload_cmd()
                    rs = "OK"
                else:
                    models = dir(commands_)
                    if func in models:
                        f = eval('commands_.%s' % func)
                        rs = json_dumps(f(*args,**kw))
                logger.info('"%s" "%s" "cmd"' % (addr[0],path) )
            elif action =='api':
                try:
                    exec("from api.%s import %s" % (query[1],query[2]))
                    f = eval(func)
                    rs = json.dumps(f(kw))
                except Exception as e:
                    rs = str(e)    
            elif action == 'cron':
                if func == 'add':
                    kw  = json_loads(html._DATA)
                    cron={}
                    cron['func'] = kw.pop('func')
                    cron['cron_info'] = kw.pop('cron_info')
                
                    if 'biz' not in kw:
                        cron['biz'] = func
                    else:
                        cron['biz'] = kw['biz']
                        
                    cron['args'] = kw
                    cronthread.add_cron(cron)
                elif func == 'remove':
                    kw = html._GET
                    cronthread.remove_cron(kw['func'],kw['biz'])
                rs = '{"status":1}'
            else:             
                kw  = json_loads(html._DATA)
                #print(html._Header['Method'],html._DATA,html._POST)
                if 'biz' not in kw:
                    kw['biz'] = func
                taskid = "%s-%s-%s" % (func,time.strftime('%Y%m%d'),int(time.time()))
                kw['pid'] = taskid
                kw['func'] = func
                
                kw['job_type'] = task_reg_model.task_list[func]['type']
                rs = task_reg_model.task_registry[func](*args,**kw)
                
                logger.info('"%s" "%s" "task" "%s"' % (addr[0],path,taskid))
            h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
            if rs:
                h = 'HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s' % (len(rs),rs)

            new_sock.send(h.encode())
            new_sock.close()
            
            self._report += 1
        except Exception as e:
            traceback.print_exc()
            errinfo = traceback.format_exc()
            #errinfo='出错了'
            logger.error('"%s" "%s" "erron" "%s"' % (addr[0],path,errinfo))
            req = 'HTTP/1.1 500 system error!\r\nContent-Length: %d\r\n\r\n%s' % (len(str(e)),str(e))
            #req = 'HTTP/1.1 500 system error!\r\n\r\n'
            new_sock.send(req.encode())
            new_sock.close()
Пример #4
0
def action(body):
    """
    """
    t=float('%0.3f'%time.time())
    try:
        path=""
        try:        
            html = HttpParse(body)
            path = html._Header['Request']
            if html._Header['Method']=='POST':
                body_len = int(html._Header['Content-Length'])
                if 'Content-Type' in html._Header and html._Header['Content-Type'].find('multipart/form-data') >-1:
                    html._parse_multipart()
                else:
                    html._parse_post()    
        except Exception as e:
            raise Exception('The request of the error')
        #路由解析
        try:
            mod, func = html.getpath()[:2]
        except:
            h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
            return h
               
        kw  = html._GET or html._POST or html.Up_File or html._DATA or {}
        if mod == 'cmd': 
            if func == 'reload_tasks':
                reload_tasks()
            elif func == 'reload_cmd':
                reload_cmd()
            else:
                models = dir(commands_)
                if func in models:
                    f = eval('commands_.%s' % func)
                    rs = json_dumps(f(kw))
            logger.info('"%s" "%s" "cmd"' % ("",path) )
        else:             
            try:
                exec("from %s import %s" % (mod, func))
                f = eval(func)
                rs = json.dumps(f(kw))
                
            except Exception as e:
                #raise e
                traceback.print_exc()
        ftime = float('%0.3f'%(time.time()-t))
        logger.info('"%s" "%s" "%s"' % ("",path,ftime) )
        #h = 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK'
        
        if rs:
            h = 'HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s' % (len(rs),rs)
        return h
        
        self._report += 1
    except Exception as e:
        traceback.print_exc()
        errinfo = traceback.format_exc()
        #errinfo='出错了'
        #logger.error('"%s" "%s" "erron" "%s"' % (addr[0],path,errinfo))
        req = 'HTTP/1.1 500 system error!\r\nContent-Length: %d\r\n\r\n%s' % (len(str(e)),str(e))
        #req = 'HTTP/1.1 500 system error!\r\n\r\n'
        return req