api-example.py 1.3 KB

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