Exemple #1
0
    def send_req(self, req):
        binary = req.SerializeToString()
        ns = nshead.NsHead()
        ns.body_len = len(binary)
        ns_binary = ns.pack()
        logging.info('send req to server [%s:%d][body_len: %d]' %
                     (self.sock.getpeername()[0], self.sock.getpeername()[1],
                      ns.body_len))
        self.sock.sendall(ns_binary + binary)
        logging.info('wait for server resp ......')
        data = ""
        while len(data) < nshead.head_size:
            new_read = self.sock.recv(nshead.head_size - len(data))
            data += new_read
            if len(new_read) == 0:
                logging.warning('server close the socket[len(data) = %d]' %
                                len(data))
                return None

        head = data[0:nshead.head_size]
        ns = nshead.NsHead()
        ns.unpack(head)
        data = data[nshead.head_size:]
        while len(data) < ns.body_len:
            new_read = self.sock.recv(ns.body_len - len(data))
            data += new_read
            if len(new_read) == 0:
                logging.warning('server close the socket[len(data) = %d]' %
                                len(data))
                return None
        resp = dc_req_pb2.response()
        resp.ParseFromString(data[:ns.body_len])
        return resp
Exemple #2
0
 def ChildRun(self):
     while True:
         signal.signal(signal.SIGALRM, self.ChildTimeOut)
         clisock, (remhost, remport) = self.srvsock.accept()
         signal.alarm(self.process_timeout_s)
         print "recv", (remhost, remport)
         while 1:
             try:
                 res_nshead = nshead.NsHead.from_str(clisock.recv(36))
                 if not res_nshead:
                     break
                 recved = 0
                 body = ''
                 while recved < res_nshead.body_len:
                     temp = clisock.recv(res_nshead.body_len - recved)
                     recved += len(temp)
                     body = body + temp
                 req = mcpack.loads(body)
                 resp = {}
                 ret = self.process_func(req, resp)
                 resp["ret"] = ret
                 head = nshead.NsHead()
                 mcpack_body = mcpack.dumps(resp)
                 head.body_len = len(mcpack_body)
                 clisock.sendall(head.pack() + mcpack_body)
             except:
                 traceback.print_exc(file=sys.stderr)
                 break
             break
         clisock.close()
         signal.alarm(0)
 def selectsvr_send(self, sendpack):
     head = nshead.NsHead()
     #mcpack_body = mcpack.dumps(packData, charset='gb18030')
     mcpack_body = mcpack.dumps(sendpack)
     head.body_len = len(mcpack_body)
     request = head.pack() + mcpack_body
     if not self.writer.Write(request):
         print " get cluster writer fail lang %d type %d url %s" % (sendpack['language'], sendpack['type'], sendpack['url'])
         return False
     return True
Exemple #4
0
def handle(req_type, text):
    #ret infomation
    ret = 0
    resp_code = 0
    resp_content = "xxx"
    #video_class server
    ip = "127.0.0.1"
    port = 9875
    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        #1.build request pack
        ##@content
        body = {
            'request_type': mcpack.INT32(req_type),
            'text': mcpack.STR(text)
        }
        mcpack_body = mcpack.dumps(body)
        ##@head
        head = nshead.NsHead()
        head.body_len = len(mcpack_body)

        #2. connect to server
        #server_sock.create_connection((ip, port))
        server_sock.connect((ip, port))
        server_sock.settimeout(60)

        #3. send request
        request = head.pack() + mcpack_body
        server_sock.sendall(request)

        #4. recv response
        res_nshead = nshead.NsHead.from_str(server_sock.recv(36))
        if res_nshead is False:
            return -3, -1, "text is invalid"
        recved = 0
        body = ''
        while recved < res_nshead.body_len:
            temp = server_sock.recv(1024)
            recved += len(temp)
            body = body + temp
        res_body = mcpack.loads(body)
        #parse response
        resp_code = res_body['response_code']
        resp_content = res_body['tags']
    except Exception as ex:
        ret = -1
        resp_code = -1
        resp_content = "maybe error in param!"
        logging.error(traceback.format_exc())
    finally:
        #5. disconnect with server
        server_sock.close()
    return ret, resp_code, resp_content
Exemple #5
0
 def ChildRun(self):
     self.BeforeChildRun()
     while True:
         sys.stdout.flush()
         sys.stderr.flush()
         clisock, (remhost, remport) = self.srvsock.accept()
         print "recv", (remhost, remport)
         while 1:
             try:
                 req_nshead = nshead.NsHead.from_str(clisock.recv(36))
                 if (not req_nshead) or req_nshead.magic_num != 0xfb709394:
                     print 'nshead read error'
                     break
                 recved = 0
                 body = ''
                 while recved < req_nshead.body_len:
                     temp = clisock.recv(req_nshead.body_len - recved)
                     recved += len(temp)
                     body = body + temp
                 req = json.loads(body, encoding='utf-8')
                 resp = {}
                 ret = self.process_func(req, resp)
                 resp["ret"] = ret
                 #mcpack_body = json.dumps(resp, ensure_ascii=False).encode('utf-8')
                 if resp.get('dodump', True):
                     mcpack_body = json.dumps(resp,
                                              ensure_ascii=resp.get(
                                                  'ensure_ascii',
                                                  False)).encode('utf-8')
                     #mcpack_body = msgpack.dumps(resp)
                 else:
                     mcpack_body = resp['dumpstr']
                 head = nshead.NsHead()
                 head.body_len = len(mcpack_body)
                 clisock.sendall(head.pack() + mcpack_body)
                 #print 'ok',mcpack_body
             except:
                 traceback.print_exc(file=sys.stderr)
                 break
             break
         clisock.close()
Exemple #6
0
    def send(self, cmd_id, keys):
        """
        description: send cmd 
        """
        try:
            if self.is_connected == False:
                self.connect()
            head = nshead.NsHead()
            head.id = cmd_id
            # TODO parse keys
            body = keys
            mcpack_body = mcpack.dumps(body)
            head.body_len = len(mcpack_body)

            if self.verbose_mode == True:
                print "Send:"
                print head
                print body

            request = head.pack() + mcpack_body
            self.sock.send(request)
        except:
            print traceback.format_exc()
            sys.exit(2)
Exemple #7
0
def cs_process(s, my_dc):
    global exit_count
    data = ""
    while True:
        create_output_per_hour()
        while len(data) < nshead.head_size:
            new_read = s.recv(4096)
            data += new_read
            if len(new_read) == 0:
                return
        head = data[0:nshead.head_size]
        ns = nshead.NsHead()
        ns.unpack(head)
        data = data[nshead.head_size:]
        while len(data) < ns.body_len:
            new_read = s.recv(4096)
            data += new_read
            if len(new_read) == 0:
                logging.info("_read_pack_: client close socket")
                return
        body = data[0:ns.body_len]
        data = data[len(body):]
        if ns.id == 10:
            logging.info('reload config')
            my_dc.reload_conf()
            continue
        # mc_dict = mcpack.loads(body)
        proto_req = dc_req_pb2.request()
        proto_req.ParseFromString(body)
        logging.info('--------------  count : %d ------------' % exit_count)

        req = request.Request.from_proto_req(proto_req)
        logging.info('begin check: [target_url: %s, cur_url: %s]' %
                     (req.target_url, req.cur_url))
        logging.debug(req)
        try:
            resp = my_dc.judge(req)
            proto_resp = dc_req_pb2.response()
            resp.to_proto_req(proto_resp)
            # log something to database
            if req.is_relive:
                relive_output_f.write(
                    '%s\t%s\t%d\t%s\n' %
                    (req.target_url, req.target_url, resp.code, resp.msg))
            else:
                output_f.write(
                    '%s\t%s\t%d\t%s\n' %
                    (req.target_url, req.target_url, resp.code, resp.msg))
            if not req.no_resp:
                binary = proto_resp.SerializeToString()
                ns = nshead.NsHead()
                ns.body_len = len(binary)
                ns_binary = ns.pack()
                logging.info('send result to socket [%s:%d]\n' %
                             s.getpeername())
                s.sendall(ns_binary + binary)
        except:
            print('--------- %s [%s]-------------' %
                  (datetime.now(), req.target_url),
                  file=sys.stderr)
            traceback.print_exc(file=sys.stderr)
            continue
        exit_count += 1
        if exit_count > max_exit_count:
            s.close()
            return
Exemple #8
0
 def server_reload_conf(self):
     ns = nshead.NsHead()
     ns.id = 10
     ns.body_len = 0
     logging.info('send server reload conf req to server')
     self.sock.sendall(ns.pack())