api-example.py 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python2.7
  2. #Short Python Example for connecting to The Cgminer API
  3. #Written By: setkeh <https://github.com/setkeh>
  4. #Thanks to Jezzz for all his Support.
  5. #NOTE: When adding a param with a pipe | in bash or ZSH you must wrap the arg in quotes
  6. #E.G "pga|0"
  7. import socket
  8. import json
  9. import sys
  10. def linesplit(socket):
  11. buffer = socket.recv(4096)
  12. done = False
  13. while not done:
  14. more = socket.recv(4096)
  15. if not more:
  16. done = True
  17. else:
  18. buffer = buffer+more
  19. if buffer:
  20. return buffer
  21. api_command = sys.argv[1].split('|')
  22. if len(sys.argv) < 3:
  23. api_ip = '127.0.0.1'
  24. api_port = 4028
  25. else:
  26. api_ip = sys.argv[2]
  27. api_port = sys.argv[3]
  28. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  29. s.connect((api_ip,int(api_port)))
  30. if len(api_command) == 2:
  31. s.send(json.dumps({"command":api_command[0],"parameter":api_command[1]}))
  32. else:
  33. s.send(json.dumps({"command":api_command[0]}))
  34. response = linesplit(s)
  35. response = response.replace('\x00','')
  36. response = json.loads(response)
  37. print response
  38. s.close()