-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgminer.py
58 lines (43 loc) · 1.19 KB
/
cgminer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import socket
import json
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
class UnavailableException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Cgminer:
def __init__(self, ip, port, verbose):
LOGGER.info('CGminer IP: %s', ip)
self.ip = ip
LOGGER.info('CGminer port: %d', port)
self.port = port
self.verbose = verbose
def send_command(self, command):
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((self.ip, int(self.port)))
request = json.dumps({'command': command})
if self.verbose:
LOGGER.info('Request: %s', request)
s.send(request)
response = self.linesplit(s)
except socket.error:
raise UnavailableException('CGminer unavailable')
s.close()
if self.verbose:
LOGGER.info('Response: %s', response)
response = response.replace('\x00', '')
response = json.loads(response)
return response[command.upper()]
def linesplit(self, socket):
buffer_size = 4096
data = socket.recv(buffer_size)
while True:
more = socket.recv(buffer_size)
if not more:
return data
else:
data = data+more