Пример #1
0
 def queryword(self, word):
     if word == None:
         return
     cursor = self.conn.cursor()
     #由于备份数据是以urlid为名称存储的,所以这里需要将urlid取出
     querystr = 'select urllist.id as urlid,urllist.title as title,wordlist.word as word,wordlist.type as wdtype,wordlocation.location as location,urllist.url as url from wordlist,wordlocation,urllist where wordlist.word = \'' + word + '\' and wordlocation.wordid = wordlist.id and wordlocation.urlid = urllist.id'
     cursor.execute(querystr)
     data = cursor.fetchall()
     datalist = []
     for one in data:
         datadict = {
             'urlid': one[0],
             'title': one[1],
             'word': one[2],
             'type': one[3],
             'location': one[4],
             'url': one[5]
         }
         datalist.append(datadict)
     cursor.close()
     logger.notice({
         'msg': 'database query info',
         'queryword': word,
         'queryresult': datalist
     })
     if len(datalist) == 0:
         return None
     return datalist
Пример #2
0
	def run(self):
		#从请求查询词中检索结果,结果保存到reslist中
		resultlist = self.__search()
		if resultlist == None:
			self.reslist = None
		else:
		#结果排序,排序后的结果保存到reslist中
			self.reslist = self.__sort(resultlist)
		logger.notice(self.reslist)
Пример #3
0
 def run(self):
     #从请求查询词中检索结果,结果保存到reslist中
     resultlist = self.__search()
     if resultlist == None:
         self.reslist = None
     else:
         #结果排序,排序后的结果保存到reslist中
         self.reslist = self.__sort(resultlist)
     logger.notice(self.reslist)
Пример #4
0
	def queryword(self,word):
		if word == None:
			return
		cursor = self.conn.cursor()
		#由于备份数据是以urlid为名称存储的,所以这里需要将urlid取出
		querystr = 'select urllist.id as urlid,urllist.title as title,wordlist.word as word,wordlist.type as wdtype,wordlocation.location as location,urllist.url as url from wordlist,wordlocation,urllist where wordlist.word = \'' + word + '\' and wordlocation.wordid = wordlist.id and wordlocation.urlid = urllist.id'
		cursor.execute(querystr)
		data = cursor.fetchall()
		datalist = []
		for one in data:
			datadict = {'urlid':one[0],'title':one[1],'word':one[2],'type':one[3],'location':one[4],'url':one[5]}
			datalist.append(datadict)
		cursor.close()
		logger.notice({'msg':'database query info','queryword':word,'queryresult':datalist})
		if len(datalist) == 0:
			return None
		return datalist
Пример #5
0
	def start(self):
		#传输格式为JSON格式
		serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		#地址重用机制,必须放在bind之前
		serversocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)	
		serversocket.bind((self.addr,self.port))
		
		serversocket.listen(self.listennum)
		#非阻塞模式
		serversocket.setblocking(0)
		#No Delay模式
		serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
		#发送接收缓冲区设置
		serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.recvbuf)
		serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.sendbuf)
		
		epoll = select.epoll()
		epoll.register(serversocket.fileno(), select.EPOLLIN)
		try:
			connections = {}; requests = {}; responses = {}
			while True:
				#超时时间
				events = epoll.poll(self.timeout)
				for fileno, event in events:
					if fileno == serversocket.fileno():
						connection, address = serversocket.accept()
						connection.setblocking(0)
						epoll.register(connection.fileno(), select.EPOLLIN)
						connections[connection.fileno()] = connection
						requests[connection.fileno()] = b''
						responses[connection.fileno()] = ''
					#监听接口可读
					elif event & select.EPOLLIN:
						requests[fileno] += connections[fileno].recv(self.recvlen)
						logger.notice({'msg':'server accept success','request':requests[fileno]})
						buildobj = builder(requests[fileno])
						epoll.modify(fileno, select.EPOLLOUT)
						response_data = buildobj.build()
						#错误处理,并不退出,需要给Server提示
						if  response_data == None:
							Logger.warning('builder return failed')
							response_data = json.dumps({'status':'2','desc':'server work error'})
						responses[fileno] = response_data
					elif event & select.EPOLLOUT:
						byteswritten = connections[fileno].send(responses[fileno])
						logger.notice({'msg':'server response ok','query':requests[fileno],'writelen':byteswritten,'result':responses[fileno][0:byteswritten]})
						responses[fileno] = responses[fileno][byteswritten:]
						if len(responses[fileno]) == 0:
							epoll.modify(fileno, 0)
							try:
								connections[fileno].shutdown(socket.SHUT_RDWR)
							except:
								pass
					
					elif event & select.EPOLLHUP:
						epoll.unregister(fileno)
						connections[fileno].close()
						del connections[fileno]
		finally:
			epoll.unregister(serversocket.fileno())
			epoll.close()
			serversocket.close()
Пример #6
0
    def start(self):
        #传输格式为JSON格式
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #地址重用机制,必须放在bind之前
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        serversocket.bind((self.addr, self.port))

        serversocket.listen(self.listennum)
        #非阻塞模式
        serversocket.setblocking(0)
        #No Delay模式
        serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        #发送接收缓冲区设置
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF,
                                self.recvbuf)
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                                self.sendbuf)

        epoll = select.epoll()
        epoll.register(serversocket.fileno(), select.EPOLLIN)
        try:
            connections = {}
            requests = {}
            responses = {}
            while True:
                #超时时间
                events = epoll.poll(self.timeout)
                for fileno, event in events:
                    if fileno == serversocket.fileno():
                        connection, address = serversocket.accept()
                        connection.setblocking(0)
                        epoll.register(connection.fileno(), select.EPOLLIN)
                        connections[connection.fileno()] = connection
                        requests[connection.fileno()] = b''
                        responses[connection.fileno()] = ''
                    #监听接口可读
                    elif event & select.EPOLLIN:
                        requests[fileno] += connections[fileno].recv(
                            self.recvlen)
                        logger.notice({
                            'msg': 'server accept success',
                            'request': requests[fileno]
                        })
                        buildobj = builder(requests[fileno])
                        epoll.modify(fileno, select.EPOLLOUT)
                        response_data = buildobj.build()
                        #错误处理,并不退出,需要给Server提示
                        if response_data == None:
                            Logger.warning('builder return failed')
                            response_data = json.dumps({
                                'status':
                                '2',
                                'desc':
                                'server work error'
                            })
                        responses[fileno] = response_data
                    elif event & select.EPOLLOUT:
                        byteswritten = connections[fileno].send(
                            responses[fileno])
                        logger.notice({
                            'msg':
                            'server response ok',
                            'query':
                            requests[fileno],
                            'writelen':
                            byteswritten,
                            'result':
                            responses[fileno][0:byteswritten]
                        })
                        responses[fileno] = responses[fileno][byteswritten:]
                        if len(responses[fileno]) == 0:
                            epoll.modify(fileno, 0)
                            try:
                                connections[fileno].shutdown(socket.SHUT_RDWR)
                            except:
                                pass

                    elif event & select.EPOLLHUP:
                        epoll.unregister(fileno)
                        connections[fileno].close()
                        del connections[fileno]
        finally:
            epoll.unregister(serversocket.fileno())
            epoll.close()
            serversocket.close()