-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcclient.py
56 lines (44 loc) · 1.88 KB
/
calcclient.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
import sys
import asyncio
import aiohttp
URL = "http://127.0.0.1:4711"
async def add(value: float):
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
data = {"value": value}
async with session.post(f"{URL}/add", headers=headers, json=data) as response:
response = await response.read()
print(f"Status: {response}")
async def add(value: float):
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
data = {"value": value}
async with session.post(f"{URL}/add", headers=headers, json=data) as response:
response = await response.read()
print(f"Status: {response}")
async def sub(value: float):
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
data = {"value": value}
async with session.post(f"{URL}/sub", headers=headers, json=data) as response:
response = await response.read()
print(f"Status: {response}")
async def result():
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
async with session.get(f"{URL}/result", headers=headers) as response:
response = await response.read()
print(f"Status: {response}")
if __name__ == "__main__":
loop = asyncio.new_event_loop()
for cmd, func in (("add", add), ("sub", sub), ("result", result)):
if cmd not in sys.argv:
continue
index = sys.argv.index(cmd)
if cmd == "add" or cmd == "sub":
loop.run_until_complete(func(float(sys.argv[index + 1])))
else:
loop.run_until_complete(func())
break
else:
print(f"Invalid syntax. Usage: {__file__} add|sub|result [value]")