Post
Topic
Board Mining (Altcoins)
Re: Official FutureBit Moonlander 2 Driver and Support Thread
by
kcw-grunt
on 11/01/2018, 19:45:53 UTC
BFGMinerRPC issues: Huh Huh Angry

I hoped others have hacked through the forest already.  RPC is completely new to me and I'm also a novice python dev.  But, I tried to use the example RPC python script to RPC into my miner and I either can't get it to receive the data and parse it or I get no response.

Here are the 2 methods I tried:

1. Using Thomas Sileo CGMiner blog bigups (https://thomassileo.name/blog/2013/09/17/playing-with-python-and-cgminer-rpc-api/)
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Tue Jan  9 11:00:14 2018

@author: kerrywashington
"""

import socket
import json


class BFGMinerAPI(object):
    """ Cgminer RPC API wrapper. """
    def __init__(self, host='mylocalhost', port=4028):
        self.data = {}
        self.host = host
        self.port = port

    def command(self, command, arg=None):
        """ Initialize a socket connection,
        send a command (a json encoded dict) and
        receive the response (and decode it).
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            sock.connect((self.host, self.port))
            payload = {"command": command}
            if arg is not None:
                # Parameter must be converted to basestring (no int)
                payload.update({'parameter': unicode(arg)})
                sock.send(json.dumps(payload))
            received = self._receive(sock)
            print(received)
        finally:
            sock.shutdown(socket.SHUT_RDWR)
            sock.close()
        
        return json.loads(received[:-1])

    def _receive(self, sock, size=4096):
        msg = ''
        while 1:
            chunk = sock.recv(size)
            if chunk:
                msg += chunk
            else:
                break
        return msg

    def __getattr__(self, attr):
        def out(arg=None):
            return self.command(attr, arg)
        return out
I started this script and tweaked it for my setup I'm remotely running the script with my server name.
The response is when I run c.command('summary') and I get no response. I set breakpoints in my Anaconda/Spyder IDE but get nothing.

                c = BFGMinerAPI()
                c.command('summary')

When I run the same script on locally..it still freezes...same behavior

2. Using the BFGAPI script:
Code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2013 Christian Berendt
# Copyright 2013 Luke Dashjr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.  See COPYING for more details.

import argparse
import json
import logging
import pprint
import socket

logging.basicConfig(
         format='%(asctime)s %(levelname)s %(message)s',
         level=logging.DEBUG
)

parser = argparse.ArgumentParser()
parser.add_argument("command", default="summary", nargs='?')
parser.add_argument("parameter", default="", nargs='?')
parser.add_argument("--hostname", default="localhost")
parser.add_argument("--port", type=int, default=4028)
args = parser.parse_args()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect((args.hostname, args.port))
    
except socket.error, e:    
    print "Error receiving data: %s" % e
    logging.error(e)

try:
    s.send("{\"command\" : \"%s\", \"parameter\" : \"%s\"}"
            % (args.command, args.parameter)
          )
except socket.error, e:
    print "Error receiving data: %s" % e
    logging.error(e)
data = ''
while True:
    try:
        newdata = s.recv(1024)
        if newdata:
            data += newdata
            print('Data:' + newdata)
        else:
            break
    except socket.error, e:
        break

try:
    s.close()
except socket.error,e:
    logging.error(e)
if data:
    data = json.loads(data.replace('\x00', ''))
    print('dat data')
    pp = pprint.PrettyPrinter()
    pp.pprint(data)



Nothing prints to output

Could anyone offer some help?  Thanks!