-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcherry.py
129 lines (105 loc) · 3.07 KB
/
cherry.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import cherrypy
import time
import subprocess
import json
import math
BitcoinDataPath = '/home/bitcoin/.bitcoin/'
BitcoinCLIPath = './bitcoin-cli.sh'
Port = 81
def executeCmd(command):
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return p.stdout.read()
def getUptime():
t = executeCmd(['cat', '/proc/uptime'])
words = t.split()
if len(words) < 2:
return -1
return float(words[0])
def getDirSize(dir):
t = executeCmd(['du', dir, '-b'])
lines = t.splitlines()
if len(lines) < 1:
return -1
line = lines[-1]
i = line.find('\t')
if i < 1:
return -1
return int(line[0:i])
def getDiskFree(dir):
t = executeCmd(['df', dir, '-B1'])
lines = t.splitlines()
if len(lines) < 1:
return -1
line = lines[-1]
words = line.split()
if len(words) < 4:
return -1
return int(words[3])
# /proc/net/netstat = all traffic from network interface since boot
# good to determine transfer speed
def getTraffic():
t = executeCmd(['cat', '/proc/net/netstat'])
lines = t.splitlines()
if len(lines) < 1:
return -1
line = lines[-1]
words = line.split()
if len(words) < 9:
return -1
DownloadBytes = int(words[7])
UploadBytes = int(words[8])
return [DownloadBytes, UploadBytes]
# getnettotals = traffic of bitcoind since start
# not good to determine transfer speed
def getTraffic_2():
data = json.loads(executeCmd([BitcoinCLIPath, 'getnettotals']))
return [data['totalbytesrecv'], data['totalbytessent']]
class MyWebServer(object):
@cherrypy.expose
def index(self):
f = open('index.html')
return f.read()
@cherrypy.expose
def traffic_json(self, WaitTime):
wait = float(WaitTime)
Traffic = getTraffic()
DownloadBytes1 = Traffic[0]
UploadBytes1 = Traffic[1]
time.sleep(wait)
Traffic = getTraffic()
DownloadBytes2 = Traffic[0]
UploadBytes2 = Traffic[1]
Downstream = int((DownloadBytes2 - DownloadBytes1) / wait)
Upstream = int((UploadBytes2 - UploadBytes1) / wait)
data = {'CurrentDownstream': Downstream, 'CurrentUpstream': Upstream, 'Uptime': getUptime(), 'Download': DownloadBytes2, 'Upload': UploadBytes2}
return json.dumps(data)
@cherrypy.expose
def general_json(self):
getinfo = executeCmd([BitcoinCLIPath, '-getinfo'])
data = {}
if getinfo == '':
data['errors'] = 'bitcoind is not running or not yet ready'
else:
data = json.loads(getinfo)
data['Uptime'] = getUptime()
data['BlockchainSize'] = getDirSize(BitcoinDataPath)
data['DiskFree'] = getDiskFree(BitcoinDataPath)
data['Peers'] = json.loads(executeCmd([BitcoinCLIPath, 'getpeerinfo']))
return json.dumps(data)
@cherrypy.expose
def favicon_ico(self):
# return ''
raise cherrypy.HTTPRedirect('https://www.bitcoincash.org/img/favicon/favicon-96x96.png')
index.exposed = True
cherrypy.config.update({
'server.socket_host' : '0.0.0.0',
'server.socket_port' : Port,
})
conf = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.abspath(os.getcwd())
}
}
cherrypy.quickstart(MyWebServer(), '/', conf)