-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonsocket.py
119 lines (106 loc) · 3.6 KB
/
jsonsocket.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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 hived_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, {}