def StartServer(self, port): import socket host = '127.0.0.1' s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, port)) while True: try: message, address = s.recvfrom(8192) self.logger.debug("len %d" % len(message)) query = Query() try: query.ParseFromString(message) except: self.logger.debug(traceback.format_exc()) self.RespQuery(1, 0, 0, [], address, s) continue self.logger.debug("get content %s" % query.query) dialogs, terms = self.Search(query.query) for d in dialogs: self.logger.debug("dialog %d weight %f" % (d[0], d[1])) for t in terms: self.logger.debug(" terms %s" % t[0]) self.RespQuery(0, dialogs, terms, address, s) except KeyboardInterrupt: break except: self.logger.error(traceback.format_exc())
def StartServer(self, port): import socket host = "127.0.0.1" s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, port)) while True: try: message, address = s.recvfrom(8192) self.logger.debug("len %d" % len(message)) query = Query() cmd = struct.unpack("!H", message[0:2])[0] message = message[2:] self.logger.debug("cmd %d" % cmd) if cmd == 1: try: query.ParseFromString(message) except: self.logger.debug("parse from string error") self.logger.debug(traceback.format_exc()) self.RespGames(1, [], [], address, s) return self.logger.debug("get content %s" % query.query) games, terms = self.Search(query.query) for game in games: self.logger.debug("game weight %f id %d" % (game[0], game[1])) for term in terms: self.logger.debug("term %s weight %f" % (term[0], term[1])) self.RespGames(0, games, terms, address, s) elif cmd == 2: gameId = struct.unpack("!I", message[0:4])[0] self.AddOneGame(gameId) else: self.logger.error("not find cmd %d" % cmd) except: self.logger.debug(traceback.format_exc())
sys.path.append(workPath + "/..") from django.core.management import setup_environ from content_engine import settings setup_environ(settings) from search_pb2 import Query, Response import socket import struct from portal.models import Game if __name__ == '__main__': for g in Game.objects.all(): print "test game Id %d" % g.id stQuery = Query() stQuery.query = g.name query = stQuery.SerializeToString() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(struct.pack("!H", 1) + query, ("127.0.0.1", 8128)) resp = s.recv(4196) stResp = Response() stResp.ParseFromString(resp) if stResp.games[0].gameId != g.id: for game in stResp.games: print "game %d name %f game %f" % (game.gameId, game.nameRel, game.gameRel) #for term in stResp.terms: # print "term %s weight %f" % (term.term, term.weight)
from content_engine import settings setup_environ(settings) from search_pb2 import Query, Response import socket import struct from portal.models import Game if __name__ == '__main__': for g in Game.objects.all(): print "test game Id %d" % g.id stQuery = Query() stQuery.query = g.name query = stQuery.SerializeToString() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(struct.pack("!H", 1) + query, ("127.0.0.1", 8128)) resp = s.recv(4196) stResp = Response() stResp.ParseFromString(resp) if stResp.games[0].gameId != g.id: for game in stResp.games: print "game %d name %f game %f" % (game.gameId, game.nameRel, game.gameRel) #for term in stResp.terms: # print "term %s weight %f" % (term.term, term.weight)
#!/usr/local/bin/python2.7 #coding: utf8 from search_pb2 import Query, ResponseDialog import socket import struct if __name__ == '__main__': stQuery = Query() stQuery.query = u"牛逼" query = stQuery.SerializeToString() print len(query) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(struct.pack("!H", 1) + query, ("127.0.0.1", 8038)) resp = s.recv(4196) stResp = ResponseDialog() stResp.ParseFromString(resp) print "result %d" % stResp.result for d in stResp.dialogs: print "dialog %d weight %f" % (d.qId, d.rel) for term in stResp.terms: print "term %s weight %f" % (term.term, term.weight)