-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
147 lines (137 loc) · 5.54 KB
/
Copy pathclient.py
File metadata and controls
147 lines (137 loc) · 5.54 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
143
144
145
146
147
from __future__ import print_function
import logging
import socket
import grpc
import trial_1_pb2
import trial_1_pb2_grpc
import random
import sys
if len(sys.argv) <2 or len(sys.argv) > 2:
print("error, please enter port number of your tenant as the only command line argument")
sys.exit()
logging.basicConfig(
filename="client.log", format="%(asctime)s %(message)s", filemode="w"
)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# IP_addr_intermediate = "172.17.63.199"
IP_addr_intermediate = "localhost"
port_intermediate = sys.argv[1]
def get_self_ip():
try:
# Create a socket object and connect to a remote host (e.g., Google's DNS server)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
# Get the local IP address
self_ip = s.getsockname()[0]
return self_ip
except socket.error as e:
print(f"Error: {e}")
return None
finally:
s.close()
def run():
ip = get_self_ip()
print("Select from the following services: ")
print("0. Echo")
print("1. Simple interest calculation")
print("2. Tax computation")
print("3. EMI calculation")
print("4. FD returns calculation")
print("5. Currency conversion")
function = int(input("Enter service number: "))
logger.debug("Client is requesting function: " + str(function))
if function == 0:
data1 = int(input("Enter value to be echoed: "))
logger.debug("Sending values: " + str(data1))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(data1=data1, function=function, ip=ip)
)
logger.debug("Echoed value: " + str(response.val))
print("Echoed value: " + str(response.val))
elif function == 1:
data1 = int(input("Enter principal value: "))
data2 = int(input("Enter tenure (in years): "))
logger.debug("Sending values: " + str(data1) + ", " + str(data2))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(
data1=data1, data2=data2, function=function, ip=ip
)
)
logger.debug("Simple interest: " + str(response.val))
print("Calculated simple interest value: " + str(response.val))
elif function == 2:
data1 = int(input("Enter annual income: "))
logger.debug("Sending values: " + str(data1))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(data1=data1, function=function, ip=ip)
)
logger.debug("Tax: " + str(response.val))
print("Computed tax value: " + str(response.val))
elif function == 3:
data1 = int(input("Enter loan amount: "))
data2 = int(input("Enter tenure (in years): "))
logger.debug("Sending values: " + str(data1) + ", " + str(data2))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(
data1=data1, data2=data2, function=function, ip=ip
)
)
logger.debug("EMI calculated: " + str(response.val))
print("Computed EMI value:: " + str(response.val))
elif function == 4:
data1 = int(input("Enter FD amount: "))
data2 = int(input("Enter tenure (in years): "))
logger.debug("Sending values: " + str(data1) + ", " + str(data2))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(
data1=data1, data2=data2, function=function, ip=ip
)
)
logger.debug("Estimated returns: " + str(response.val))
print("Estimated returns on FD: " + str(response.val))
elif function == 5:
data1 = int(input("Enter money (in INR): "))
print("The follwoing target currencies are available: ")
print("1. USD")
print("2. EUR")
print("3. GBP")
print("4. JPY")
data2 = int(input("Enter your selection: "))
logger.debug("Sending values: " + str(data1) + ", " + str(data2))
with grpc.insecure_channel(
IP_addr_intermediate + ":" + port_intermediate
) as channel:
stub = trial_1_pb2_grpc.AlertStub(channel)
response = stub.InvokeMethod(
trial_1_pb2.function_message(
data1=data1, data2=data2, function=function, ip=ip
)
)
logger.debug("Converted value: " + str(response.val))
print("Converted value in the target currency: " + str(response.val))
else:
print("Invalid function number")
logger.debug("Invalid function number given: " + str(function))
if __name__ == "__main__":
run()