Browse Source

added example for Python using the RPC API

Christian Berendt 13 years ago
parent
commit
52369e06ef
3 changed files with 60 additions and 2 deletions
  1. 2 1
      Makefile.am
  2. 6 1
      README.RPC
  3. 52 0
      api-example.py

+ 2 - 1
Makefile.am

@@ -4,7 +4,8 @@ ACLOCAL_AMFLAGS = -I m4
 EXTRA_DIST	= example.conf m4/gnulib-cache.m4 linux-usb-bfgminer \
 EXTRA_DIST	= example.conf m4/gnulib-cache.m4 linux-usb-bfgminer \
 		  api-example.php miner.php	\
 		  api-example.php miner.php	\
 		  API.class API.java api-example.c windows-build.txt \
 		  API.class API.java api-example.c windows-build.txt \
-		  bitstreams/* README.FPGA README.RPC README.scrypt
+		  bitstreams/* README.FPGA README.RPC README.scrypt \
+                  api-example.py
 
 
 SUBDIRS		= lib ccan
 SUBDIRS		= lib ccan
 
 

+ 6 - 1
README.RPC

@@ -379,7 +379,7 @@ with the values after the '='
 If you enable BFGMiner debug (--debug or using RPC), you will also get messages
 If you enable BFGMiner debug (--debug or using RPC), you will also get messages
 showing some details of the requests received and the replies
 showing some details of the requests received and the replies
 
 
-There are included 4 program examples for accessing the API:
+There are included 5 program examples for accessing the API:
 
 
 api-example.php - a PHP script to access the API
 api-example.php - a PHP script to access the API
   usage: php api-example.php command
   usage: php api-example.php command
@@ -404,6 +404,11 @@ miner.php - an example web page to access the API
  See the end of this README.RPC for details of how to tune the display
  See the end of this README.RPC for details of how to tune the display
  and also to use the option to display a multi-rig summary
  and also to use the option to display a multi-rig summary
 
 
+api-example.py - a Python script to access the API
+  usage: python api-example.py [--host HOST] [--port PORT] [command] [parameter]
+ by default it sends a 'summary' request to the miner at 127.0.0.1:4028
+ If you specify a command it will send that request instead
+
 ----------
 ----------
 
 
 Feature Changelog for external applications using the API:
 Feature Changelog for external applications using the API:

+ 52 - 0
api-example.py

@@ -0,0 +1,52 @@
+#!/usr/bin/python
+
+# author: Christian Berendt <berendt@b1-systems.de>
+
+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:
+    logging.error(e)
+
+try:
+    s.send("{\"command\" : \"%s\", \"parameter\" : \"%s\"}"
+            % (args.command, args.parameter)
+          )
+except socket.error, e:
+    logging.error(e)
+
+
+data = None
+try:
+    data = s.recv(1024)
+except socket.error, e:
+    logging.error(e)
+
+try:
+    s.close()
+except socket.error,e:
+    logging.error(e)
+
+if data:
+    data = json.loads(data.replace('\x00', ''))
+    pp = pprint.PrettyPrinter()
+    pp.pprint(data)