How to use json to get data from MPBM.It would have saved me quite a few hours if an example of this type had been available, so, here it is for anyone else who might be looking.
The 'magic' involved is having to set the headers on the request in order to receive a non-null response.
E.g.,
curl <url> -d "1" -H "DNT: 1" -H "If-Modified-Since:Sat, 1 Jan 2000 00:00:00 GMT" -H "Content-Type:application/json"
You can find examples of required <url> in the Python example below.
This gives a quick test to ensure the web-server / mpbm is working before you get bogged down in "why is urllib3 throwing that exception?".
(BTW, "requests" is a third party library. Free to use it seems. You can find it if you need it.)
--
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Path hack.
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
import json
import unittest
import pickle
import tempfile
from pprint import pprint
import datetime
import time
import requests
from requests.compat import str, StringIO
from requests import HTTPError
from requests import get, post, head, put
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from requests.exceptions import InvalidURL
# curl <url> -d "1" -H "DNT: 1" -H "If-Modified-Since:Sat, 1 Jan 2000 00:00:00 GMT" -H "Content-Type:application/json"
def getDataFromMPBM():
auth = HTTPBasicAuth('admin', 'mpbm')
headers = {'content-type' : 'application/json', 'DNT' : '1', 'If-Modified-Since' : 'Sat, 1 Jan 2000 00:00:00 GMT' }
workerURL = '
http://host.domain.com:8832/api/statsgadget/getworkerstats'
blockchainURL = '
http://host.domain.com:8832/api/statsgadget/getblockchainstats'
worksourceURL = '
http://host.domain.com:8832/api/statsgadget/getworksourcestats'
allURL = '
http://host.domain.com:8832/api/statsgadget/getallstats'
r = requests.post(workerURL, auth=auth, data='1', headers=headers, prefetch=True)
#print(json.dumps( r.json, sort_keys = True, indent = 4 ) )
workerResponse = json.loads( r.text )
#pprint( workerResponse )
return workerResponse
tempHigh = 0.0
tempLow = 100.0
while( True ):
workerResponse = getDataFromMPBM()
startTime = datetime.datetime.fromtimestamp( workerResponse[ 'starttime' ] )
dataTime = datetime.datetime.fromtimestamp( workerResponse[ 'timestamp' ] )
upTime = dataTime - startTime
tempNow = workerResponse['workers'][0]['temperature']
if tempNow < tempLow : tempLow = tempNow
if tempNow > tempHigh : tempHigh = tempNow
#print( upTime )
print( '{0:.1f} \tHigh: {2:.1f} \tLow: {3:.1f} \t{1}'.format( tempNow, dataTime, tempHigh, tempLow ) )
time.sleep( 9.991 )
exit()