-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
68 lines (51 loc) · 1.85 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Modified from
# https://docs.python.org/3.8/library/socketserver.html#asynchronous-mixins
import socket
import json
import time
import argparse
def client_session(host, port) -> None:
# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((host, port))
print("=" * 50)
print("QA Client")
print("=" * 50)
while True:
text = input("Please Input Text: \n")
print("-" * 50)
question = input("Please Input Question: \n")
print("-" * 50)
data_dict = {"text": text, "question": question}
data = json.dumps(data_dict)
sock.sendall(bytes(data, "utf-8"))
# Receive data from the server and shut down
start_time = time.time()
received = str(sock.recv(1024), "utf-8")
end_time = time.time()
latency = (end_time - start_time) * 1000
print("Answer: ")
print(received)
print("(Latency: {} ms)".format(latency))
print("-" * 50)
def main() -> None:
host_default = "localhost"
port_default = 9999
parser = argparse.ArgumentParser(
description="Question and answer server.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--host",
type=str,
help="Default host IP.",
default=host_default)
parser.add_argument("--port",
type=int,
help="Default port ID.",
default=port_default)
argv = parser.parse_args()
host = argv.host
port = argv.port
client_session(host=host, port=port)
if __name__ == "__main__":
main()