-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-rpc.py
More file actions
49 lines (41 loc) · 1.39 KB
/
json-rpc.py
File metadata and controls
49 lines (41 loc) · 1.39 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
"""
Using the JSON-RPC API (POST interface)—more powerful and structured
This method requires an API key and supports signed responses, quotas, and a richer protocol
GitHub RANDOM.ORG
Direct use via HTTP POST
"""
import requests
import json
def fetch_random_jsonrpc(api_key, num, minimum, maximum, signed=False):
"""
Uses JSON-RPC API to get random integers.
If signed=True, returns signed response.
"""
method = "generateSignedIntegers" if signed else "generateIntegers"
payload = {
"jsonrpc": "2.0",
"method": method,
"params": {
"apiKey": api_key,
"n": num,
"min": minimum,
"max": maximum,
"replacement": True
},
"id": 1
}
headers = {"Content-Type": "application/json"}
resp = requests.post("https://api.random.org/json-rpc/4/invoke", json=payload, headers=headers)
resp.raise_for_status()
data = resp.json()
if "error" in data:
raise RuntimeError(f"random.org error: {data['error']}")
result = data.get("result") or data.get("random")
integers = result["data"]
signature = data.get("signature") if signed else None
return integers, signature
# Example usage:
if __name__ == "__main__":
api_key = "YOUR_API_KEY_HERE"
nums, sig = fetch_random_jsonrpc(api_key, 5, 0, 100, signed=False)
print("Random:", nums, "Signature:", sig)