-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
142 lines (116 loc) · 4.27 KB
/
client.py
File metadata and controls
142 lines (116 loc) · 4.27 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from datetime import datetime
from xmlrpc.client import Fault
from xmlrpc.client import ProtocolError
from xmlrpc.client import ServerProxy
SERVER_URL = "http://127.0.0.1:8000"
def ask(prompt, required=True):
while True:
try:
value = input(prompt).strip()
except KeyboardInterrupt:
print("\ninput canceled")
return ""
except EOFError:
print("\ninput stream closed")
return ""
if not required or value:
return value
print("Input is required.")
def safe_rpc_call(proxy, method_name, *args):
method = getattr(proxy, method_name)
try:
return method(*args)
except Fault as exc:
return {"ok": False, "message": f"rpc fault: {exc}", "data": {}}
except ProtocolError as exc:
return {"ok": False, "message": f"protocol error: {exc}", "data": {}}
except OSError as exc:
return {"ok": False, "message": f"connection error: {exc}", "data": {}}
except Exception as exc:
return {"ok": False, "message": f"unexpected rpc error: {exc}", "data": {}}
def print_response(response):
if not isinstance(response, dict):
print("\n--- response ---")
print("status: error")
print("message: invalid response from server")
print("----------------\n")
return
ok = response.get("ok", False)
message = response.get("message", "")
data = response.get("data", {})
print("\n--- response ---")
print("status:", "ok" if ok else "error")
print("message:", message)
if data:
print("data:")
print(data)
print("----------------\n")
def menu():
print("1) add note")
print("2) get topic")
print("3) search wikipedia")
print("4) append wikipedia result to topic")
print("5) Exit")
return ask("Select an option: ")
def run_client(url):
try:
proxy_context = ServerProxy(url, allow_none=True)
except Exception as exc:
print(f"failed to create client: {exc}")
return
with proxy_context as proxy:
ping = safe_rpc_call(proxy, "ping")
if not ping.get("ok"):
print("server is not available")
print("reason:", ping.get("message", "unknown error"))
return
print("connected:", url)
while True:
choice = menu()
if choice == "1":
topic = ask("topic name: ")
note_name = ask("note name: ")
text = ask("text: ")
timestamp = ask(
"timestamp (empty = now): ",
required=False,
)
if not timestamp:
timestamp = datetime.now().strftime("%m/%d/%y - %H:%M:%S")
if not topic or not note_name or not text:
print("missing required values")
continue
response = safe_rpc_call(proxy, "add_note", topic, note_name, text, timestamp)
print_response(response)
elif choice == "2":
topic = ask("topic name: ")
if not topic:
print("missing topic name")
continue
response = safe_rpc_call(proxy, "get_topic", topic)
print_response(response)
elif choice == "3":
term = ask("wikipedia term: ")
if not term:
print("missing wikipedia term")
continue
response = safe_rpc_call(proxy, "search_wikipedia", term)
print_response(response)
elif choice == "4":
topic = ask("topic name: ")
note_name = ask("note name for wiki data: ")
term = ask("wikipedia term: ")
if not topic or not note_name or not term:
print("missing required values")
continue
response = safe_rpc_call(proxy, "append_wikipedia_to_topic", topic, note_name, term)
print_response(response)
elif choice == "5":
print("bye")
break
else:
print("invalid choice, use 1-5")
def main():
run_client(SERVER_URL)
if __name__ == "__main__":
main()