Skip to content

Commit

Permalink
JSONSocket changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mkochanowicz committed Mar 5, 2018
1 parent e9dec18 commit a871354
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 115 deletions.
223 changes: 119 additions & 104 deletions python_scripts/tests/api_tests/jsonsocket.py
Original file line number Diff line number Diff line change
@@ -1,104 +1,119 @@
#!/usr/bin/env python3
import json
import socket

class JSONSocket(object):
"""
Class encapsulates socket object with special handling json rpc.
timeout is ignored now - nonblicking socket not supported.
"""
def __init__(self, host, port, path, timeout=None):
"""
host in form [http[s]://]<ip_address>[:port]
if port not in host must be spefified as argument
"""
if host.find("http://") == 0 or host.find("https://") == 0:
host = host[host.find("//")+2 : len(host)]
_host = host
if port != None:
host = host + ":" + str(port)
else:
colon_pos = host.rfind(":")
_host = host[0 : colon_pos]
port = int(host[colon_pos+1 : len(host)])
self.__fullpath = host + path
self.__host = bytes(host, "utf-8")
self.__path = bytes(path, "utf-8")
#print("<host>: {}; <port>: {}".format(_host, port))
self.__sock = socket.create_connection((_host, port))
self.__sock.setblocking(True)
#self.__sock.settimeout(timeout)
self.__head = b"POST " + self.__path + b" HTTP/1.0\r\n" + \
b"HOST: " + self.__host + b"\r\n" + \
b"Content-type: application/json\r\n"

def get_fullpath(self):
return self.__fullpath

def request(self, data=None, json=None):
"""
data - complete binary form of json request (ends with '\r\n'
json - json request as python dict
return value in form of json response as python dict
"""
if data == None:
data = bytes(json.dumps(json), "utf-8") + b"\r\n"
length = bytes(str(len(data)), "utf-8")
request = self.__head + \
b"Content-length: " + length + b"\r\n\r\n" + \
data
#print("request:", request.decode("utf-8"))
self.__sock.sendall(request)
#self.__sock.send(request)
status, response = self.__read()
#if response == {}:
# print("response is empty for request:", request.decode("utf-8"))
return status, response

def __call__(self, data=None, json=None):
return self.request(data, json)

def __read(self):
response = ''
while True:
temp = self.__sock.recv(4096)
if not temp: break
response += temp.decode("utf-8")

if response.find("HTTP") == 0:
response = response[response.find("\r\n\r\n")+4 : len(response)]
if response and response != '':
r = json.loads(response)
if 'result' in r:
return True, r
else:
return False, r

return False, {}

def __del__(self):
if self.__sock:
self.__sock.close()


def steemd_call(host, data=None, json=None, max_tries=10, timeout=0.1):
"""
host - [http[s]://<ip_address>:<port>
data - binary form of request body, if missing json object should be provided (as python dict/array)
"""
try:
jsocket = JSONSocket(host, None, "/rpc", timeout)
except:
print("Cannot open socket for:", host)
return False, {}

for i in range(max_tries):
try:
status, response = jsocket(data, json)
if status:
return status, response
except:
continue
else:
return False, {}
#!/usr/bin/env python3
import sys
import json
import socket

class JSONSocket(object):
"""
Class encapsulates socket object with special handling json rpc.
timeout is ignored now - nonblicking socket not supported.
"""
def __init__(self, host, port, path, timeout=None):
"""
host in form [http[s]://]<ip_address>[:port]
if port not in host must be spefified as argument
"""
self.__sock = None
if host.find("http://") == 0 or host.find("https://") == 0:
host = host[host.find("//")+2 : len(host)]
_host = host
if port != None:
host = host + ":" + str(port)
else:
colon_pos = host.rfind(":")
_host = host[0 : colon_pos]
port = int(host[colon_pos+1 : len(host)])
self.__fullpath = host + path
self.__host = bytes(host, "utf-8")
self.__path = bytes(path, "utf-8")
#print("<host>: {}; <port>: {}".format(_host, port))
self.__sock = socket.create_connection((_host, port))
self.__sock.setblocking(True)
#self.__sock.settimeout(timeout)
self.__head = b"POST " + self.__path + b" HTTP/1.0\r\n" + \
b"HOST: " + self.__host + b"\r\n" + \
b"Content-type: application/json\r\n"

def get_fullpath(self):
return self.__fullpath

def request(self, data=None, json=None):
"""
data - complete binary form of json request (ends with '\r\n'
json - json request as python dict
return value in form of json response as python dict
"""
if data == None:
data = bytes(json.dumps(json), "utf-8") + b"\r\n"
length = bytes(str(len(data)), "utf-8")
request = self.__head + \
b"Content-length: " + length + b"\r\n\r\n" + \
data
#print("request:", request.decode("utf-8"))
self.__sock.sendall(request)
#self.__sock.send(request)
status, response = self.__read()
#if response == {}:
# print("response is empty for request:", request.decode("utf-8"))
return status, response

def __call__(self, data=None, json=None):
return self.request(data, json)

def __read(self):
response = ''
binary = b''
while True:
temp = self.__sock.recv(4096)
if not temp: break
binary += temp

response = binary.decode("utf-8")

if response.find("HTTP") == 0:
response = response[response.find("\r\n\r\n")+4 : len(response)]
if response and response != '':
r = json.loads(response)
if 'result' in r:
return True, r
else:
return False, r

return False, {}

def __del__(self):
if self.__sock:
self.__sock.close()


def steemd_call(host, data=None, json=None, max_tries=10, timeout=0.1):
"""
host - [http[s]://<ip_address>:<port>
data - binary form of request body, if missing json object should be provided (as python dict/array)
"""
# try:
# jsocket = JSONSocket(host, None, "/rpc", timeout)
# except:
# print("Cannot open socket for:", host)
# return False, {}

for i in range(max_tries):
try:
jsocket = JSONSocket(host, None, "/rpc", timeout)
except:
type, value, traceback = sys.exc_info()
print("Error: {}:{} {} {}".format(1, type, value, traceback))
print("Error: Cannot open JSONSocket for:", host)
continue
try:
status, response = jsocket(data, json)
if status:
return status, response
except:
type, value, traceback = sys.exc_info()
print("Error: {}:{} {} {}".format(1, type, value, traceback))
print("Error: JSONSocket request failed for: {} ({})".format(host, data.decode("utf-8")))
continue
else:
return False, {}
2 changes: 1 addition & 1 deletion python_scripts/tests/api_tests/list_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def main():


if __name__ == "__main__":
main()
main()
17 changes: 11 additions & 6 deletions python_scripts/tests/api_tests/test_ah_get_account_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import json
import os
import shutil
import re
import locale
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import Future
Expand Down Expand Up @@ -46,11 +48,12 @@ def main():
wdir = Path(sys.argv[4])

accounts_file = sys.argv[5] if len( sys.argv ) > 5 else ""

if accounts_file != "":
name = re.compile("[^\r^\n]*")
try:
with open(accounts_file, "r") as file:
accounts = [account for account in file]
with open(accounts_file, "rt") as file:
accounts = [name.match(account).group(0) for account in file]
except:
exit("Cannot open file: " + accounts_file)
else:
Expand Down Expand Up @@ -115,6 +118,7 @@ def compare_results(url1, url2, accounts, max_tries=10, timeout=0.1):

def get_account_history(url1, url2, account, max_tries=10, timeout=0.1):
global wdir
global errors
START = -1
HARD_LIMIT = 10000
LIMIT = HARD_LIMIT
Expand All @@ -138,8 +142,9 @@ def get_account_history(url1, url2, account, max_tries=10, timeout=0.1):

if status1 == False or status2 == False or json1 != json2:
print("Comparison failed for account: {}; start: {}; limit: {}".format(account, START, LIMIT))
errors += 1

filename = wdir / account
++errors
try: file = filename.open("w")
except: print("Cannot open file:", filename); return False

Expand All @@ -158,7 +163,7 @@ def get_account_history(url1, url2, account, max_tries=10, timeout=0.1):

if last == 0: break

--last
last -= 1
START = last
LIMIT = last if last < HARD_LIMIT else HARD_LIMIT
# while True
Expand All @@ -167,4 +172,4 @@ def get_account_history(url1, url2, account, max_tries=10, timeout=0.1):


if __name__ == "__main__":
main()
main()
8 changes: 4 additions & 4 deletions python_scripts/tests/api_tests/test_ah_get_ops_in_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""
Usage: script_name jobs url1 url2 [last_block [first_block]]
Example: script_name 4 http://127.0.0.1:8090 http://127.0.0.1:8091 5000000 0
Usage: script_name jobs url1 url2 [wdir [last_block [first_block]]]
Example: script_name 4 http://127.0.0.1:8090 http://127.0.0.1:8091 ./ 5000000 0
set jobs to 0 if you want use all processors
if last_block == 0, it is read from url1 (as reference)
"""
Expand Down Expand Up @@ -154,7 +154,7 @@ def compare_results(f_block, l_block, url1, url2, max_tries=10, timeout=0.1):

if status1 == False or status2 == False or json1 != json2:
print("Difference @block: {}\n".format(i))
++errors
errors += 1

filename = wdir / Path(str(f_block) + "_" + str(l_block) + ".log")
try: file = filename.open( "w" )
Expand All @@ -175,4 +175,4 @@ def compare_results(f_block, l_block, url1, url2, max_tries=10, timeout=0.1):


if __name__ == "__main__":
main()
main()

0 comments on commit a871354

Please sign in to comment.