File: api-example.py

package info (click to toggle)
bfgminer 4.7.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,116 kB
  • ctags: 9,129
  • sloc: ansic: 62,470; lisp: 5,221; sh: 4,572; php: 2,645; asm: 667; makefile: 427; python: 85; ruby: 23
file content (62 lines) | stat: -rwxr-xr-x 1,510 bytes parent folder | download
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
59
60
61
62
#!/usr/bin/python
# Copyright 2013 Christian Berendt
# Copyright 2013 Luke Dashjr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.  See COPYING for more details.

import argparse
import json
import logging
import pprint
import socket

logging.basicConfig(
         format='%(asctime)s %(levelname)s %(message)s',
         level=logging.DEBUG
)

parser = argparse.ArgumentParser()
parser.add_argument("command", default="summary", nargs='?')
parser.add_argument("parameter", default="", nargs='?')
parser.add_argument("--hostname", default="localhost")
parser.add_argument("--port", type=int, default=4028)
args = parser.parse_args()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.connect((args.hostname, args.port))
except socket.error, e:
    logging.error(e)

try:
    s.send("{\"command\" : \"%s\", \"parameter\" : \"%s\"}"
            % (args.command, args.parameter)
          )
except socket.error, e:
    logging.error(e)


data = ''
while True:
    try:
        newdata = s.recv(1024)
        if newdata:
            data += newdata
        else:
            break
    except socket.error, e:
        break

try:
    s.close()
except socket.error,e:
    logging.error(e)

if data:
    data = json.loads(data.replace('\x00', ''))
    pp = pprint.PrettyPrinter()
    pp.pprint(data)