0

I am trying to extract data from an in-house project(http:\192.168.1.15:8082). It uses json-rpc 2.0. At first, I tried using beautilsoup to retrieve the information since it was being displayed on the screen, but that does not work, all I get is a bunch o function code. I am not familiar with json-rpc. Any help is welcome.

The REPONSE data printed on the website look like this:

RESPONSE:

{"jsonrpc":"2.0","result":[{"uri":"Geometry","time":1537525006,"geo":{"type":"characteristic","id":125,"information2":{"type":"Point","Weight":[15.362154,196.623546]}

How do I retrieve this information?

Thank you all,

Nick,

Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
  • What do you mean by extract data? Is there an API to query? if so try using `requests` - http://docs.python-requests.org/en/master/ – Craicerjack Sep 21 '18 at 10:31

1 Answers1

0

Take a look at requests https://pypi.org/project/requests

To obey the standard you need to put in the payload to your request but otherwise it is just a normal post

import requests
import json

def main():
    url = "http://192.168.1.15:8082/jsonrpc"
    headers = {'content-type': 'application/json'}

    # Example echo method
    payload = {
        "method": "your_method_name",
        "jsonrpc": "2.0",
        "id": 0,
    }
    response = requests.post(
        url, data=json.dumps(payload), headers=headers).json()

    print(response)
    print(response['result']['uri'])
    print(response['result']['time'])
    print(response['result']['geo'])
    print(response['result']['geo']['id'])
    print(response['result']['geo']['type'])
    print(response['result']['geo']['information2'])
    print(response['result']['geo']['information2']['type'])
    print(response['result']['geo']['information2']['Weight'])

if __name__ == "__main__":
    main()

If you want something that is specifically json_rpc then take a look at jsonrpc_requests https://pypi.org/project/jsonrpc-requests/
Notice in this one how the deserialization of the response is handled for you and you just get back the result of the method call rather than the whole response.

from jsonrpc_requests import Server

def main():
    url = "http://localhost:8082/jsonrpc"
    server = Server(url)
    result  = server.dosomething()


    print(result)
    print(result['uri'])
    print(result['time'])
    print(result['geo'])
    print(result['geo']['id'])
    print(result['geo']['type'])
    print(result['geo']['information2'])
    print(result['geo']['information2']['type'])
    print(result['geo']['information2']['Weight'])

if __name__ == "__main__":
    main()
Tim Hughes
  • 3,144
  • 2
  • 24
  • 24