forked from lukepark327/onechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
65 lines (48 loc) · 1.5 KB
/
agent.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
63
64
65
import json
from requests import get, post
import ast
from pprint import pprint
URL = "http://127.0.0.1"
PORT = 3001
def getBlockchain():
op = "blocks"
target = URL + ":" + str(PORT) + '/' + op
res = get(target)
return res
def addNewBlock(req=None):
op = "mineBlock"
target = URL + ":" + str(PORT) + '/' + op
if req == None:
res = post(target)
else:
headers = {'Content-type': 'application/json'}
data = {"data": req}
res = post(target, data=json.dumps(data), headers=headers)
return res
def getPeers():
op = "peers"
target = URL + ":" + str(PORT) + '/' + op
res = get(target)
return res
def addPeer(req=None):
op = "addPeer"
target = URL + ":" + str(PORT) + '/' + op
headers = {'Content-type': 'application/json'}
data = {"peer": req}
res = post(target, data=json.dumps(data), headers=headers)
return res
def stopNode():
op = "stop"
target = URL + ":" + str(PORT) + '/' + op
res = post(target)
return res
if __name__ == '__main__':
"""
res = getBlockchain() # pprint(ast.literal_eval(res.text)[0]['data'])
res = addNewBlock(req="Anything")
res = getPeers() # pprint(ast.literal_eval(res.text)[0])
res = addPeer(req="ws://127.0.0.1:6003")
res = stopNode() # pprint(ast.literal_eval(res.text)['msg'])
print(res.text)
"""
pass