Last active
May 16, 2024 12:19
-
-
Save Deadlyelder/6baad86e832acf0df23a70914c014d7a to your computer and use it in GitHub Desktop.
A python script that has as a class Class script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import time, requests, json | |
class RPCHost(object): | |
def __init__(self, url): | |
self._session = requests.Session() | |
self._url = url | |
self._headers = {'content-type': 'application/json'} | |
def call(self, rpcMethod, *params): | |
payload = json.dumps({"method": rpcMethod, "params": list(params), "jsonrpc": "2.0"}) | |
tries = 5 | |
hadConnectionFailures = False | |
while True: | |
try: | |
response = self._session.post(self._url, headers=self._headers, data=payload) | |
except requests.exceptions.ConnectionError: | |
tries -= 1 | |
if tries == 0: | |
raise Exception('Failed to connect for remote procedure call.') | |
hadFailedConnections = True | |
print("Couldn't connect for remote procedure call, will sleep for five seconds and then try again ({} more tries)".format(tries)) | |
time.sleep(10) | |
else: | |
if hadConnectionFailures: | |
print('Connected for remote procedure call after retry.') | |
break | |
if not response.status_code in (200, 500): | |
raise Exception('RPC connection failure: ' + str(response.status_code) + ' ' + response.reason) | |
responseJSON = response.json() | |
if 'error' in responseJSON and responseJSON['error'] != None: | |
raise Exception('Error in RPC call: ' + str(responseJSON['error'])) | |
return responseJSON['result'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment