-
Notifications
You must be signed in to change notification settings - Fork 10
/
liquid_rpc_class.py
36 lines (34 loc) · 1.58 KB
/
liquid_rpc_class.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
from __future__ import print_function
import time, requests, json, re
class RPCHost(object):
def __init__(self, url):
self._session = requests.Session()
if re.match(r'.*\.onion/*.*', url):
self._session.proxies = {}
self._session.proxies['http'] = 'socks5h://localhost:9050'
self._session.proxies['https'] = 'socks5h://localhost:9050'
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
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']