test_httpclient.py 1.9 KB
Newer Older
B
barrierye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing

H
HexToString 已提交
16
from paddle_serving_client.httpclient import HttpClient
H
HexToString 已提交
17
import sys
W
wangjiawei04 已提交
18
import numpy as np
H
HexToString 已提交
19 20
import time

H
HexToString 已提交
21
client = HttpClient()
H
HexToString 已提交
22
client.load_client_config(sys.argv[1])
H
HexToString 已提交
23 24 25 26 27 28 29
''' 
if you want use GRPC-client, set_use_grpc_client(True)
or you can directly use client.grpc_client_predict(...)
as for HTTP-client,set_use_grpc_client(False)(which is default)
or you can directly use client.http_client_predict(...)
'''
#client.set_use_grpc_client(True)
H
HexToString 已提交
30 31 32
'''
if you want to enable Encrypt Module,uncommenting the following line
'''
H
HexToString 已提交
33
#client.use_key("./key")
H
HexToString 已提交
34 35 36 37 38
'''
if you want to compress,uncommenting the following line
'''
#client.set_response_compress(True)
#client.set_request_compress(True)
H
HexToString 已提交
39 40 41 42
'''
we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False
'''
H
HexToString 已提交
43
#client.set_http_proto(True)
H
HexToString 已提交
44 45
client.connect(["127.0.0.1:9393"])
fetch_list = client.get_fetch_names()
H
HexToString 已提交
46

H
HexToString 已提交
47 48 49 50 51
import paddle
test_reader = paddle.batch(
    paddle.reader.shuffle(
        paddle.dataset.uci_housing.test(), buf_size=500),
    batch_size=1)
W
wangjiawei04 已提交
52
for data in test_reader():
H
HexToString 已提交
53
    new_data = np.zeros((1, 13)).astype("float32")
W
wangjiawei04 已提交
54
    new_data[0] = data[0][0]
H
HexToString 已提交
55
    fetch_map = client.predict(
H
HexToString 已提交
56
        feed={"x": new_data}, fetch=fetch_list, batch=True)
W
wangjiawei04 已提交
57
    print(fetch_map)
H
HexToString 已提交
58
    break