def DoFormat(self, request):

        craw_file = request.text
        craw_file = json.loads(craw_file)
        if craw_file:
            self.db.connect('crawl_data')
            msg = ''
            if craw_file:
                print(craw_file)
                saved_files = self.conn_db(craw_file)
                corpus, main_id, counts = self.get_corpus(saved_files)

                if corpus:
                    try:
                        dictionary, tfidf_vectors = self.get_Model(corpus)
                        similar = self.get_similar(craw_file, dictionary, tfidf_vectors)
                        msg = self.insert_or_counts(similar, craw_file, main_id, counts)
                    except:
                        self.db.execute(self.insert, [craw_file['content'], craw_file['date'], craw_file['id'], \
                                               craw_file['tags'], craw_file['time'], craw_file['update_time'], \
                                               craw_file['url'], craw_file['website']])
                        msg = u'插入成功'
            self.db.close()
            return data_pb2.Data(text=msg)
        else:
            return data_pb2.Data(text='None')
Beispiel #2
0
def run_client():
    channel = grpc.insecure_channel(ADDRESS)
    client = data_pb2_grpc.FormatDataStub(channel)

    for _ in range(10):
        response = client.doFormat(data_pb2.Data(test="hello world"))
        print("Result:", response.test)
Beispiel #3
0
    def send(self):
        # data = datagram_pb2.Data()
        data = data_pb2.Data()
        data.seq_num = self.seq_num
        data.send_ts = curr_ts_ms()
        data.sent_bytes = self.sent_bytes
        data.delivered_time = self.delivered_time
        data.delivered = self.delivered
        data.payload = self.dummy_payload

        packet = self.buffer.pop(0)
        data.frame_id = packet.frame_id  # fixed32 occupy 6 bytes, totally add 18 bytes
        data.frame_start_packet_seq = packet.frame_start_packet_seq
        data.frame_end_packet_seq = packet.frame_end_packet_seq
        data.codec_bitrate = packet.codec_bitrate

        data.send_time = time.time()  # second
        serialized_data = data.SerializeToString()

        info = "seq_num:" + str(data.seq_num) + " send_ts:" + str(data.send_time) + " frame_id:" + str(
            data.frame_id) + " frame_start:" + str(data.frame_start_packet_seq) + " frame_end:" + str(
            data.frame_end_packet_seq) + "\n"

        # self.f.write(info)

        self.sock.sendto(serialized_data, self.peer_addr)
        self.seq_num += 1
        self.sent_bytes += len(serialized_data)

        self.one_second_data += len(serialized_data)
Beispiel #4
0
def run():
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    img = np.ones((2, 2, 3), dtype=np.uint8) * 22
    
    client = data_pb2_grpc.TransferImageStub(channel=conn)
    response = client.DoTransfer(data_pb2.Data(shape=bytes(img.shape)))
    print("received: ", tuple(response.shape))
Beispiel #5
0
 def DoTransfer(self, request, context):
     # get the image from client request
     #img_shape = tuple(request.shape)
     start = timer()
     re_array = np.frombuffer(request.image, np.uint8)
     print('received client request of image array length: ', len(re_array))
     # Convert back the data to original image shape.
     re_img = cv2.imdecode(re_array, 1)
     #while main_q.qsize() > 10:
     #     main_q.get()
     main_q.put(re_img)
     encimg = processed_q.get()
     #if main_q.empty():
     #     main_q.put(re_img)
     #if not processed_q.empty():
     #     encimg = processed_q.get()
     #else:
     #     encimg = cv2.resize(re_img, (512, 512))
     #     result, encimg = cv2.imencode('.jpg', encimg)
     #print('doing pose estimation...')
     print('Returning to client...')
     res = data_pb2.Data(image=encimg.tobytes())
     end = timer()
     print('used time:', end - start, 'res len:', len(res.image))
     return res
Beispiel #6
0
def run(country, city, map_info=None):
    if map_info:
        text = country + '&' + city + '&' + map_info
    else:
        text = country + '&' + city
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = data_pb2_grpc.FormatDataStub(channel=conn)
    response = client.DoFormat(data_pb2.Data(text=text))
    print("received: " + response.text)
Beispiel #7
0
def clientDetect(client, image):
    message = pickle.dumps(image)
    message = base64.b64encode(message)
    print('Send a image to model. ')
    response = client.dnet_detect(data_pb2.Data(text=message))
    result = response.text
    result = base64.b64decode(result)
    result = pickle.loads(result)
    #print('Received result. ', result)
    return result
Beispiel #8
0
def sendToRabbitMQ(msg_byte, channel, time_init):
    decoded = decode_serial(msg_byte)
    data = data_pb2.Data()
    
    data.Location = ""
    data.Entity_Id = decoded
    data.Timestamp = str(time.time() - time_init)
    to_rabbit = data.SerializeToString()
    print("Sending to RabbitMQ...")
    channel.basic_publish(exchange='', routing_key='location', body= to_rabbit)
    print("Sent: {}", to_rabbit)
Beispiel #9
0
    def dnet_detect(self, request, context):
        str = request.text
        print('Received data from client: ', str)
        #return data_pb2.Data(text = str.upper())
        my_class = base64.b64decode(str)
        my_class = pickle.loads(my_class)
        print('After decoding, my_class.value = ', my_class.value)
        my_class.value = os.getpid()
        str = pickle.dumps(my_class)
        str = base64.b64encode(str)

        return data_pb2.Data(text=str)
Beispiel #10
0
def run():
    myclass = MyClass(3)
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = data_pb2_grpc.DnetPredictStub(channel=conn)
    #print('Send: ' + _TEXT)
    print('Send, my_class.value = ', myclass.value)
    message = pickle.dumps(myclass)
    message = base64.b64encode(message)
    response = client.dnet_detect(data_pb2.Data(text=message))
    result = response.text
    result = base64.b64decode(response.text)
    result = pickle.loads(result)
    print('Received, my_class.value = ', result.value)
Beispiel #11
0
def received(ch, method, properties, body):
    print(f'Message received: {body}')
    data = data_pb2.Data()
    data.ParseFromString(body)
    to_client = {
        'Location': data.Location,
        'Entity_Id': data.Entity_Id,
        'Timestamp': data.Timestamp
    }
    #Data read. Log timestamp;
    #TODO: Make a log and timestamp function
    log_timestamp(to_client, file_log, time_t0)
    send_new_position(to_client)
Beispiel #12
0
    def dnet_detect(self, request, context):

        start = time()

        str = request.text
        image = base64.b64decode(str)
        image = pickle.loads(image)
        return_queue = self.model_pool.predictImage(image)
        result = return_queue.get()
        result = pickle.dumps(result)
        result = base64.b64encode(result)

        #print('WORKOR: {} done one image in: {}'.format(os.getpid(), time() - start))

        return data_pb2.Data(text=result)
Beispiel #13
0
def transfer(img):
    #img = np.ones((2, 2, 3), dtype=np.uint8) * 22
    # print(1)
    # img_bytes = np.ndarray.tobytes(img)
    img_bytes = img.tobytes()
    # print(2)
    #client = data_pb2_grpc.TransferImageStub(channel=conn)
    # np.array(img.shape)
    data = data_pb2.Data(image=img_bytes)
    # print('start transfer img via gRPC of shape:', img.shape)
    # print(3)
    response = client.DoTransfer(data)
    # print(4)
    # img_shape = tuple(response.shape)
    print("received transfered image size: ", len(response.image))
    img_new = cv2.imdecode(np.fromstring(response.image, np.uint8), 1)
    print("proceed transfered image: ", tuple(img_new.shape))
    # re_img = np.reshape(img_new, img_shape)
    return img_new
Beispiel #14
0
def sendToRabbitMQ(msg_byte, channel, time_init):
    house_rooms = {
        "0x1699a": "entrance",
        "0x169d6": "room1",
        "0x16a31": "room2",
        "0x169c6": "kitchen",
        "0x1929c": "bathroom",
        "0x169d2": "bedroom"
    }
    decoded = decode_serial(msg_byte)
    data = data_pb2.Data()

    data.Location = house_rooms[decoded["hex"]]
    data.Entity_Id = decoded["dec"]
    data.Timestamp = str(time.time() - time_init)
    to_rabbit = data.SerializeToString()
    print("Sending to RabbitMQ...")
    channel.basic_publish(exchange='', routing_key='location', body=to_rabbit)
    print("Sent: {}", to_rabbit)
Beispiel #15
0
def run(country,city,map_info=None,province=None):
    if not province:
        #print ('asd')
        if map_info:
            text = country + '&' + city + '&' + map_info
        else:
            text = country + '&' + city
    else:
        if map_info:
            text = country + '&' + city + '&' + map_info + '~' + province
            #print (text)
        else:
            text = country + '&' + city + '~' + province
            #print('a',text)
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = data_pb2_grpc.FormatDataStub(channel=conn)
    response = client.DoFormat(data_pb2.Data(text=text))
    print("received: " + response.text)
    return response.text
Beispiel #16
0
def sendToAddress(msg_byte, addr, time_init):
    house_rooms = {
        "0x1699a": "entrance",
        "0x169d6": "room1",
        "0x16a31": "room2",
        "0x169c6": "kitchen",
        "0x1929c": "bathroom",
        "0x169d2": "bedroom"
    }
    decoded = decode_serial(msg_byte)
    data = data_pb2.Data()
    data.Location = house_rooms[decoded["hex"]] or "unknown"
    data.Entity_Id = decoded["dec"]
    data.Timestamp = str(time.time() - time_init)
    protobuf_data = data.SerializeToString()
    print(addr)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(addr)
        s.settimeout(1.0)
        s.sendall(protobuf_data)
 def get_craw(self):
     conn = grpc.insecure_channel('192.168.1.100' + ':' + '2333')
     client = data_pb2_grpc.FormatDataStub(channel=conn)
     response = client.DoFormat(data_pb2.Data(text='getData'))
     craw_file = json.loads(response.text)
     return craw_file
Beispiel #18
0
 def DoFormat(self, request, context):
     str = request.text
     return data_pb2.Data(text=str.upper())
Beispiel #19
0
 def DoFormat(self, request, context):
     str = request.text
     print('Received data from client: ', str)
     return data_pb2.Data(text = str.upper())
Beispiel #20
0
 def doFormat(self, request, context):
     text = request.test
     return data_pb2.Data(test=text.upper())
Beispiel #21
0
def run():
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)
    client = data_pb2_grpc.FormatDataStub(channel=conn)
    response = client.DoFormat(data_pb2.Data(text='hello,world!'))
    print("received: " + response.text)
Beispiel #22
0
def run():
	conn = grpc.insecure_channel(_HOST + ':' + _PORT)
	client = data_pb2_grpc.FormatDataStub(channel = conn)
	print('Send: ' + _TEXT)
	response = client.DoFormat(data_pb2.Data(text = _TEXT))
	print('Received: ' + response.text)