SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/env python | |
| 2 | # -*- coding: utf-8 -*- | |
| 3 | ||
| 4 | #Many thanks to Thomas Sileo for this post http://thomassileo.com/blog/2013/09/17/playing-with-python-and-cgminer-rpc-api/ | |
| 5 | #Copyright (C) 2014 Martin Vool | |
| 6 | # | |
| 7 | #This program is free software; you can redistribute it and/or | |
| 8 | #modify it under the terms of the GNU General Public License | |
| 9 | #as published by the Free Software Foundation; version 2 | |
| 10 | # | |
| 11 | #This program is distributed in the hope that it will be useful, | |
| 12 | #but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | #GNU General Public License for more details. | |
| 15 | # | |
| 16 | #You should have received a copy of the GNU General Public License | |
| 17 | #along with this program; if not, write to the Free Software | |
| 18 | #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | # | |
| 20 | #Copy of the GPLv2 is located at http://www.gnu.org/licenses/gpl-2.0.html | |
| 21 | ||
| 22 | #Version 1.0 | |
| 23 | #For usage see http://wiki.loovsys.eu/index.php/Cgminer_API_python_client_for_bash_and_shell_script_for_profiling_when_system_is_idle | |
| 24 | ||
| 25 | ||
| 26 | import socket | |
| 27 | import json | |
| 28 | import fileinput | |
| 29 | ||
| 30 | ||
| 31 | def commander(cmd,ip): | |
| 32 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| 33 | sock.connect((ip, 4028)) | |
| 34 | sock.send(json.dumps(cmd)) | |
| 35 | resp = '' | |
| 36 | while 1: | |
| 37 | buf = sock.recv(4096) | |
| 38 | if buf: | |
| 39 | resp += buf | |
| 40 | else: | |
| 41 | break | |
| 42 | ||
| 43 | values=json.loads(resp[:-1]) | |
| 44 | sock.shutdown(socket.SHUT_RDWR) | |
| 45 | sock.close() | |
| 46 | return values | |
| 47 | ||
| 48 | data='' | |
| 49 | for line in fileinput.input(): | |
| 50 | data= '%s%s'%(data,line) | |
| 51 | oneline=' '.join(data.split()) | |
| 52 | - | print 'data: %s'%data #remove |
| 52 | + | |
| 53 | ||
| 54 | alldata=data.split('@',1)
| |
| 55 | ip=alldata[-1] | |
| 56 | splt=alldata[0].split(':', 1)
| |
| 57 | if len(splt) == 1: | |
| 58 | cmd={'command':splt[0]}
| |
| 59 | else: | |
| 60 | cmd={'command':splt[0],'parameter':splt[1]}
| |
| 61 | ||
| 62 | foo=commander(cmd,ip) | |
| 63 | ||
| 64 | for k,v in foo.iteritems(): | |
| 65 | print '---------------' | |
| 66 | print '%s'%k | |
| 67 | try: | |
| 68 | for vv in v: | |
| 69 | print '---------------' | |
| 70 | try: | |
| 71 | for kkk,vvv in vv.iteritems(): | |
| 72 | print '%s: %s'%(kkk,vvv) | |
| 73 | except: | |
| 74 | print '%s'%vv | |
| 75 | except: | |
| 76 | print '%s'%v |