/ api-example.py
api-example.py
 1  #!/usr/bin/env python2.7
 2  
 3  # Copyright 2013 Setkeh Mkfr
 4  #
 5  # This program is free software; you can redistribute it and/or modify it under
 6  # the terms of the GNU General Public License as published by the Free Software
 7  # Foundation; either version 3 of the License, or (at your option) any later
 8  # version.  See COPYING for more details.
 9  
10  #Short Python Example for connecting to The Cgminer API
11  #Written By: setkeh <https://github.com/setkeh>
12  #Thanks to Jezzz for all his Support.
13  #NOTE: When adding a param with a pipe | in bash or ZSH you must wrap the arg in quotes
14  #E.G "pga|0"
15  
16  import socket
17  import json
18  import sys
19  
20  def linesplit(socket):
21  	buffer = socket.recv(4096)
22  	done = False
23  	while not done:
24  		more = socket.recv(4096)
25  		if not more:
26  			done = True
27  		else:
28  			buffer = buffer+more
29  	if buffer:
30  		return buffer
31  
32  api_command = sys.argv[1].split('|')
33  
34  if len(sys.argv) < 3:
35  	api_ip = '127.0.0.1'
36  	api_port = 4028
37  else:
38  	api_ip = sys.argv[2]
39  	api_port = sys.argv[3]
40  
41  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
42  s.connect((api_ip,int(api_port)))
43  if len(api_command) == 2:
44  	s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]}))
45  else:
46  	s.send(json.dumps({"command":api_command[0]}))
47  
48  response = linesplit(s)
49  response = response.replace('\x00','')
50  response = json.loads(response)
51  print response
52  s.close()