-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path8-json_api.py
More file actions
executable file
·35 lines (30 loc) · 851 Bytes
/
Copy path8-json_api.py
File metadata and controls
executable file
·35 lines (30 loc) · 851 Bytes
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
#!/usr/bin/python3
"""
Send a POST request to localhost:5000/search_user
with a query string and print the JSON result to
standard output
"""
from requests import post
from sys import argv
def send_query(query_string: str) -> str:
"""
Send the POST questy_string to server
Args:
query_string (str): the query string
Return:
string
"""
server = "http://0.0.0.0:5000/search_user"
response = post(server, data={'q': query_string}).text
try:
resp_json = eval(response)
if len(resp_json) != 0:
return "[{}] {}".format(resp_json.get('id'), resp_json.get('name'))
return "No result"
except Exception as e:
return "Not a valid JSON"
if __name__ == "__main__":
if len(argv) >= 2:
print(send_query(argv[1]))
else:
print(send_query(""))