0

I am trying to make simple script in Python to request API GET responses from Bitcoin mining machines. I need to request a certain JSON key and value {"command":"summary"}. I will extract data from the JSON payload obtained with this to monitor some machines on Zabbix.

If I send the request with bash like this:

# echo '{"cmd":"summary"}' | timeout 1.5 nc IPADDRESS PORT

I will get the data I need, and I could even process it with bash using tr, sed and jq...

However if I send the request with my Python Script I always get "invalid msg". I would much rather do it with Python because I am learning and I can imagine my error is pretty dumb. This is the python code.

#!/usr/bin/python3
import sys
import logging
import requests
import json
server = 'http://10.136.132.140:4028'
payload = {
"command":"summary"
}

jsonpayload = json.dumps(payload)
print(payload)
print(jsonpayload)
response = requests.get(server,
jsonpayload
)
print(response.json())

This won't work no matter what I do. I have tried using directly payload as params for requests.get to send the string only. Nothing works. Everytime I get the same invalid msg. I have tried typing response = requests.get('http://IPADDRESS:PORT', '{"command":"summary"}') directly, any combination possible of single quotes or double quotes... Nothing works.

I get three exceptions when I call it. The important tracebacks are the following:

http.client.BadStatusLine: STATUS=E,When=1608811559,Code=14,Msg=invalid cmd,Description=whatsminer v1.1

urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('STATUS=E,When=1608811559,Code=14,Msg=invalid cmd,Description=whatsminer v1.1'))

raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('STATUS=E,When=1608811559,Code=14,Msg=invalid cmd,Description=whatsminer v1.1'))

What am I doing wrong here? Thanks for your help in advance!

Javi Hache
  • 11
  • 3

1 Answers1

0

If you want to send a json payload, try this:

server = '' # server url
payload = {
"command":"summary"
}
response = requests.get(server, json=payload)
Mark7888
  • 823
  • 1
  • 8
  • 20
  • That doesn't work. I had tried that already. I tried again after your comment. I get the same error. There's something about the formatting... I can't understand it. – Javi Hache Dec 24 '20 at 12:54
  • what is the `content-type` of the server? Maybe it's not `application/json`. Your error message says: 'Connection aborted.'. So maybe the server "didn't like" your payload. Maybe try something like this: `headers = {'Content-type': 'application/json'}` `requests.get(server, json=payload, headers=headers)` – Mark7888 Dec 24 '20 at 14:15
  • Hey Mark, thanks again for your help! I tried this as well to no avail. The content type should be application/json. This is a Bitcoin Miner machine which uses the cgminer program and this is the API I am calling. There are many programs that call this API without problems... and specially intriguing is the fact that I can call the API with netcat, by just passing the string... – Javi Hache Dec 24 '20 at 14:55
  • I'm linking the docs here: https://requests.readthedocs.io/en/master/user/quickstart/#make-a-request You may be mixing up the get/post/put methods with the keyword arguments params/data/json. get usually uses params and post uses data and json. But give the docs a read and see what it is exactly you should be using. – Jonathan Leon Dec 24 '20 at 20:25