aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/client.py
blob: 14b3cf99ac6981eb79d2483145520fa8179f0ea2 (plain)
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
import socket
import random
import json

HOST = "127.0.0.1"
PORT = 60000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

req = {
    "jsonrpc": "2.0",
    "id": str(random.randint(0, 1000)),
    "method": "Calc.add",
    "params": {"x": 4, "y": 3},
}
print("Send: ", req)
s.sendall(json.dumps(req).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)

req = {
    "jsonrpc": "2.0",
    "id": str(random.randint(0, 1000)),
    "method": "Calc.sub",
    "params": {"x": 4, "y": 3},
}
print("Send: ", req)
s.sendall(json.dumps(req).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)

req = {
    "jsonrpc": "2.0",
    "id": str(random.randint(0, 1000)),
    "method": "Calc.ping",
    "params": None,
}
print("Send: ", req)
s.sendall(json.dumps(req).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)

req = {
    "jsonrpc": "2.0",
    "id": str(random.randint(0, 1000)),
    "method": "Calc.version",
    "params": None,
}
print("Send: ", req)
s.sendall(json.dumps(req).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)

s.close()