This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mrpc.py
62 lines (49 loc) · 1.69 KB
/
mrpc.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
57
58
59
60
61
62
import requests
import json
import logging
logging.basicConfig(level=logging.DEBUG)
commands=('info_height','info_name','info_block_age','info_p2p_status','info_region','info_summary','info_location','info_version','peer_addr')
class mrpc:
def __init__(self,miner):
self.session = requests.Session()
self.host = 'http://'+miner
self.header = {'Content-type': 'application/json'}
self.payload = {"jsonrpc":"2.0",
"id":1,
"method":"",
"params":""}
def get(self,cmd,params=[]):
self.payload["method"]=cmd
self.payload["params"]=params
try:
response=self.session.post(self.host, json=self.payload, headers=self.header).json()['result']
except KeyError:
response=self.session.post(self.host, json=self.payload, headers=self.header).json()
return(response)
def address(self):
miner_addr = self.get('peer_addr')['peer_addr'].split('/p2p/')[1]
return miner_addr
def name(self):
miner_name = self.get('info_name')['name']
return miner_name
def height(self):
block_height = self.get('info_height')['height']
return block_height
def region(self):
hotspot_region = self.get('info_region')['region']
return hotspot_region
def test_all():
m=mrpc()
for cmd in commands:
try:
print(cmd+'\n',json.dumps(m.get(cmd), indent = 1))
print('\n')
except:
print('error',cmd)
continue
if __name__ == "__main__":
m=mrpc()
print(m.address())
print(m.name())
print(m.height())
print(test_all())