-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonclient.py
118 lines (95 loc) · 3.33 KB
/
jsonclient.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
#git tag: v1.1-pypi
#git tag: v1.1-pypi
""" contains a json object message passing server and client """
import json
import socket
import struct
import logging
import requests
logger = logging.getLogger("jsonSocket")
logger.setLevel(logging.DEBUG)
FORMAT = '[%(asctime)-15s][%(levelname)s][%(funcName)s] %(message)s'
logging.basicConfig(format=FORMAT)
class JsonSocket(object):
def __init__(self, address='127.0.0.1', port=5489):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn = self.socket
self._timeout = None
self._address = address
self._port = port
def sendObj(self, obj):
msg = json.dumps(obj)
if self.socket:
frmt = "=%ds" % len(msg)
packedMsg = struct.pack(frmt, msg)
packedHdr = struct.pack('=I', len(packedMsg))
self._send(packedHdr)
self._send(packedMsg)
def _send(self, msg):
sent = 0
while sent < len(msg):
sent += self.conn.send(msg[sent:])
def _read(self, size):
data = ''
while len(data) < size:
dataTmp = self.conn.recv(size-len(data))
data += dataTmp
if dataTmp == '':
raise RuntimeError("socket connection broken")
return data
def _msgLength(self):
d = self._read(4)
s = struct.unpack('=I', d)
return s[0]
def readObj(self):
size = self._msgLength()
data = self._read(size)
frmt = "=%ds" % size
msg = struct.unpack(frmt,data)
return json.loads(msg[0])
def close(self):
logger.debug("closing main socket")
self._closeSocket()
if self.socket is not self.conn:
logger.debug("closing connection socket")
self._closeConnection()
def _closeSocket(self):
self.socket.close()
def _closeConnection(self):
self.conn.close()
def _get_timeout(self):
return self._timeout
def _set_timeout(self, timeout):
self._timeout = timeout
self.socket.settimeout(timeout)
def _get_address(self):
return self._address
def _set_address(self, address):
pass
def _get_port(self):
return self._port
def _set_port(self, port):
pass
timeout = property(_get_timeout, _set_timeout,doc='Get/set the socket timeout')
address = property(_get_address, _set_address,doc='read only property socket address')
port = property(_get_port, _set_port,doc='read only property socket port')
class JsonClient(JsonSocket):
def __init__(self, address='127.0.0.1', port=3000):
super(JsonClient, self).__init__(address, port)
def connect(self):
for i in range(10):
try:
self.socket.connect( (self.address, self.port) )
jsonurl = requests.get("http://127.0.0.1:3000/axa/articles")
thelistuphere = []
#print jsonurl.json()
for item in jsonurl.json():
thelistuphere.append(item["title"])
print thelistuphere
except socket.error as msg:
logger.error("SockThread Error: %s" % msg)
time.sleep(3)
continue
logger.info("...Socket Connected")
return True
return False