Python Api to Read Stat of Antminer (Cgiminer)

import socketimport jsonimport requestsdef stats(ip):    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    sock.settimeout(1)    try:        sock.connect((ip, 4028))    except Exception as e:        print ip, e.message        return False    # https://github.com/ckolivas/cgminer/blob/master/API-README    sock.send(json.dumps({'command': 'stats'}))    resp = ''    while 1:        buf = sock.recv(4096)        if buf:            resp += buf        else:            break    sock.shutdown(socket.SHUT_RDWR)    sock.close()    result = json.loads(resp[:-1].replace('}{', '},{'))    '''    output = json.dumps(result, indent=2)    file = open('c:/code/test/output.txt', 'w')    file.write(output)    file.close()    '''    data = {'ip': ip}    stats = result['STATS'][0]    data['item_type'] = stats['Type']    '''    IS_D3 = False    IS_S9 = False    if data['item_type'] == 'Antminer D3':        IS_D3 = True    elif data['item_type'] == 'Antminer S9':        IS_S9 = True    '''    stats = result['STATS'][1]    data['hashrate_5s'] = stats['GHS 5s']    data['hashrate_av'] = stats['GHS av']    fan_num = 0    for i in range(8):        index = i+1        key = 'fan'+str(index)        if key in stats:            v = stats['fan'+str(index)]            if v:                fan_num += 1                data['fan'+str(fan_num)] = v    hw = {}    for i in range(16):        index = i+1        key = 'chain_acn'+str(index)        if key in stats:            v = stats['chain_acn'+str(index)]            if v:                # print 'chain_acn'+str(index), v                hw[index] = {'acn': v}    chip_num = 0    for key, value in hw.iteritems():        value['temp'] = stats['temp'+str(key)]        value['rate'] = stats['chain_rate'+str(key)]        chip_num += 1        data['temp'+str(chip_num)] = value['temp']        data['hashrate'+str(chip_num)] = value['rate']    return data

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.