From 314798f15f698cadbc7489f31ba8635eed9a6492 Mon Sep 17 00:00:00 2001 From: setkeh Date: Tue, 18 Jun 2013 23:03:30 +1000 Subject: [PATCH 1/6] Added Python Api Example --- api-example.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 api-example.py diff --git a/api-example.py b/api-example.py new file mode 100644 index 00000000..4a883896 --- /dev/null +++ b/api-example.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python2.7 + +#Short Python Example for connecting to The Cgminer API +#Written By: setkeh + +import socket +import json +import sys + +def linesplit(socket): + buffer = socket.recv(4096) + done = False + while not done: + more = socket.recv(4096) + if not more: + done = True + else: + buffer = buffer+more + if buffer: + return buffer +api_command = raw_input("Enter Api Command: ") +api_param = raw_input("Enter Api Param: ") +reply_command = raw_input("Enter Json Reply command: ") +reply_param = raw_input("Enter Json Reply Param: ") +s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) +s.connect(('192.168.1.6',4028)) +s.send(json.dumps({"command":api_command,"parameter":api_param})) +response = linesplit(s) +response = response.replace('\x00','') +response = json.loads(response) +#print response +print response[reply_command][0][reply_param] +s.close() From b5667488b32a02277c1ee9a2eb761f1bfa5df058 Mon Sep 17 00:00:00 2001 From: setkeh Date: Tue, 18 Jun 2013 23:04:18 +1000 Subject: [PATCH 2/6] Added Python Api Example --- api-example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api-example.py b/api-example.py index 4a883896..c083b21b 100644 --- a/api-example.py +++ b/api-example.py @@ -23,7 +23,7 @@ api_param = raw_input("Enter Api Param: ") reply_command = raw_input("Enter Json Reply command: ") reply_param = raw_input("Enter Json Reply Param: ") s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) -s.connect(('192.168.1.6',4028)) +s.connect(('127.0.0.1',4028)) s.send(json.dumps({"command":api_command,"parameter":api_param})) response = linesplit(s) response = response.replace('\x00','') From eca6ca8d31553a254f8b5f3d3912747f988130b4 Mon Sep 17 00:00:00 2001 From: setkeh Date: Wed, 19 Jun 2013 00:28:35 +1000 Subject: [PATCH 3/6] Fixed Python Example --- api-example.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/api-example.py b/api-example.py index c083b21b..edb32492 100644 --- a/api-example.py +++ b/api-example.py @@ -17,17 +17,24 @@ def linesplit(socket): else: buffer = buffer+more if buffer: - return buffer -api_command = raw_input("Enter Api Command: ") -api_param = raw_input("Enter Api Param: ") -reply_command = raw_input("Enter Json Reply command: ") -reply_param = raw_input("Enter Json Reply Param: ") + return buffer + +api_command = sys.argv[1].split('|') +#api_ip = sys.argv[3] +#api_port = sys.argv[4] + +if len(sys.argv) < 3: + api_ip = '127.0.0.1' + api_port = 4028 +else: + api_ip = sys.argv[2] + api_port = sys.argv[3] + s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) -s.connect(('127.0.0.1',4028)) -s.send(json.dumps({"command":api_command,"parameter":api_param})) +s.connect((api_ip,int(api_port))) +s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]})) response = linesplit(s) response = response.replace('\x00','') response = json.loads(response) -#print response -print response[reply_command][0][reply_param] +print response s.close() From 8dde833b6fefe13e94e52c9cbb7f1c3278cb2b55 Mon Sep 17 00:00:00 2001 From: setkeh Date: Wed, 19 Jun 2013 00:41:55 +1000 Subject: [PATCH 4/6] Odd Issues --- api-example.py | 1 + 1 file changed, 1 insertion(+) diff --git a/api-example.py b/api-example.py index edb32492..5181798f 100644 --- a/api-example.py +++ b/api-example.py @@ -30,6 +30,7 @@ else: api_ip = sys.argv[2] api_port = sys.argv[3] +print api_ip, api_port s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((api_ip,int(api_port))) s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]})) From 4384fe0f0ebeb3ddfa486b1bda29302f3d76297e Mon Sep 17 00:00:00 2001 From: setkeh Date: Wed, 19 Jun 2013 00:58:26 +1000 Subject: [PATCH 5/6] Fixed Commands with No params --- api-example.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/api-example.py b/api-example.py index 5181798f..16ded909 100644 --- a/api-example.py +++ b/api-example.py @@ -2,6 +2,9 @@ #Short Python Example for connecting to The Cgminer API #Written By: setkeh +#Thanks to Jezzz for all his Support. +#NOTE: When adding a param with a pipe | in bash or ZSH you must wrap the arg in quotes +#E.G "pga|0" import socket import json @@ -20,8 +23,6 @@ def linesplit(socket): return buffer api_command = sys.argv[1].split('|') -#api_ip = sys.argv[3] -#api_port = sys.argv[4] if len(sys.argv) < 3: api_ip = '127.0.0.1' @@ -30,10 +31,13 @@ else: api_ip = sys.argv[2] api_port = sys.argv[3] -print api_ip, api_port s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((api_ip,int(api_port))) -s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]})) +if len(api_command) == 2: + s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]})) +else: + s.send(json.dumps({"command":api_command[0]})) + response = linesplit(s) response = response.replace('\x00','') response = json.loads(response) From 0bdabc18958fa0eefa0743ef3842b949c2e73fa9 Mon Sep 17 00:00:00 2001 From: setkeh Date: Wed, 19 Jun 2013 14:31:30 +1000 Subject: [PATCH 6/6] Added Licence --- api-example.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api-example.py b/api-example.py index 16ded909..5b8cbb2d 100644 --- a/api-example.py +++ b/api-example.py @@ -1,5 +1,12 @@ #!/usr/bin/env python2.7 +# Copyright 2013 Setkeh Mkfr +# +# 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. + #Short Python Example for connecting to The Cgminer API #Written By: setkeh #Thanks to Jezzz for all his Support.