示例#1
0
 def query(self,head,jsonbody):
     
     #fill body len
     pack=mcpack.dumps_version(mcpack.mcpackv2,jsonbody)
     head._body_len=len(pack)
     header=head.pack()
     
     
     #server list random keep balance
     #just a shuffle
     splice=random.randint(0,len(self.serlist))
     for host,port in self.serlist[splice:]+self.serlist[0:splice]:
         try:
             #every sock in a single thread
             clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             clisock.settimeout(self.timeout)
             clisock.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
             #send and recv                
             clisock.connect((host, port))
             clisock.send(header)
             clisock.send(pack)
             return  self.recv_all(clisock)
         except Exception as e:
             #logging.warn("Exception:%s host:%s ip:%d head:%s req:%s" %(e,host,port,head,jsonbody))
             str_e=traceback.format_exc().replace('\n', ' ')
             #logging.warn(str_e)
         # try next time
         time.sleep(1)
     
     # error occur
     return None
示例#2
0
 def send_client(cls, clisock, jsonbody):
     head = PyNshead()
     pack = mcpack.dumps_version(mcpack.mcpackv2,jsonbody)
     head._body_len=len(pack)
     header=head.pack()
     clisock.send(header)
     clisock.send(pack)
     clisock.close()
示例#3
0
 def send_and_recv(self, HEAD, jsonbody):
     #·â×°pack
     pack = mcpack.dumps_version(mcpack.mcpackv2, jsonbody)
     #·â×°head
     plen = len(pack)
     provider1 = 0x6363  #0x6b636170646e6573
     provider2 = 0x0031
     #        head=struct.pack('HHHQQIII',hid,version,log_id,provider1,provider2,magic_num,0,plen)
     head = struct.pack('HHHQQIII', HEAD["id"], HEAD['version'],
                        HEAD['log_id'], HEAD['provider'], provider2,
                        HEAD['magic_num'], HEAD['reserved'], plen)
     try:
         self.sock.send(head)
         self.sock.send(pack)
         time.sleep(0.1)
         return self.recv_all()
     except Exception, e:
         print e
         raise e
示例#4
0
 def recv_all(cls,clisock,reply=None):
     #header
     header=cls.recv(clisock, 36)
     if not header:
         #logging.info("recv header timeout")
         clisock.close()
         return None
     
     nshead=PyNshead.unpack(header)
     body_len=nshead._body_len
     
     #body
     if body_len <=0:
         #logging.info("recv empty body")
         clisock.close()
         return {}
     
     dat=cls.recv(clisock,body_len)
     
     if not dat:
         #logging.info("recv data timeout,body_len %d" % body_len )
         clisock.close()
         return None
     #send reply
     if reply==None:
         clisock.close()
     else:
         pack=mcpack.dumps_version(mcpack.mcpackv2,reply)
         nshead._body_len=len(pack)
         header=nshead.pack()
         clisock.send(header)
         clisock.send(pack)
         clisock.close()
     
     #pack to json
     bodydict=mcpack.loads(dat)
     return bodydict
示例#5
0
def getparser(title):#,content,aid,pubtime):
    pok=0
    termstr=""
    tryTimes=0;
    __terms=""
    while(tryTimes < 3):
        tryTimes=tryTimes+1
        #try:
        if(1):
            con=None
            if(con==None):
                dbtype = "socket"
                dbfile = ""
                dbwto = 1000
                dbrto = 1000
                logger = Logger.initlog("./con_sock_log")

                con = UbClient.Connector(dbtype, dbhost, dbport, dbfile ,dbwto, dbrto, logger) 
                con.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
        
            reqdata = {}

            reqdata["title"]=title  
            reqdata["content"]=title
            reqdata["aid"]=0
            reqdata["pubtime"]=0

            req_pack = mcpack.dumps_version(mcpack.mcpackv1, reqdata, 4*1024*1024)
            con.write_pack(req_pack)
            (ret ,res_pack) = con.read_pack()
            pok=1
            if ret == 0:
                resdict = mcpack.loads(res_pack)
                
                __terms=resdict["terms"].replace("'"," ") 
                __terms = __terms.decode("gbk").encode('utf-8','ignore')  
                __termCount=resdict["termCount"]

                #print title
                #print __terms

    #                if(__terms!=""):
    #                    #print '-sock-ok',aid,__dupaids
    #                    dup_old_id=0
    #                    arr = __terms.split(';')
    #                    termcount=len(arr)
    #
    #                    reali=0;
    #                    _iterator_lock.acquire()  
    #                    _iterator_count+=1
    #                    reali=_iterator_count
    #                    lock.release() 
    #
    #                    if(reali % 10001 ==0):
    #                        print reali
    #
    #
    #                    if((termcount-1) !=int(__termCount)):
    #                        print "count not match",aid,termcount,__termCount
    #
    #                    termstr=__terms
                break;

        #except:
        if(0):
            print 'except-sock-',sys.exc_info()[0]
            con.sock.close()
            sleep(1)#con.sock.close()
            con=None

        if(con!=None):
            con.sock.close()
            con=None

    return __terms,__termCount
 def encode(self):
     send_data = mcpack.dumps_version(mcpack.mcpackv2, self.body, 1000000)
     self.head['body_len'] = len(send_data)
     send_data = struct.pack("HHI16sIII", self.head["id"], self.head["version"], self.head["log_id"], self.head["provider"], self.head["magic_num"], self.head["reserved"], self.head["body_len"]) + send_data
     return send_data
示例#7
0
 def send_pack(self, sock):
     pack = mcpack.dumps_version(mcpack.mcpackv2, self.resBody)
     self.resHead._body_len=len(pack)
     header = self.resHead.pack()
     sock.send(header)
     sock.send(pack)