tinterface: use parse_json - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 9efc25bdba3d56d13dfc79975cf228afcec85e00 DIR parent 9a07c1cb446fc8aad4205b0e8a34a995bdefb4da HTML Author: ThomasV <thomasv@gitorious> Date: Sat, 26 Jul 2014 16:24:22 +0200 interface: use parse_json Diffstat: M lib/daemon.py | 11 +---------- M lib/interface.py | 16 +++++++--------- M lib/network_proxy.py | 4 ++-- M lib/util.py | 12 ++++++++++++ 4 files changed, 22 insertions(+), 21 deletions(-) --- DIR diff --git a/lib/daemon.py b/lib/daemon.py t@@ -25,7 +25,7 @@ import traceback import json import Queue from network import Network -from util import print_error, print_stderr +from util import print_error, print_stderr, parse_json from simple_config import SimpleConfig t@@ -39,15 +39,6 @@ Network also reports status changes DAEMON_PORT=8001 -def parse_json(message): - n = message.find('\n') - if n==-1: - return None, message - try: - j = json.loads( message[0:n] ) - except: - j = None - return j, message[n+1:] DIR diff --git a/lib/interface.py b/lib/interface.py t@@ -33,7 +33,7 @@ DEFAULT_TIMEOUT = 5 proxy_modes = ['socks4', 'socks5', 'http'] - +from util import parse_json def cert_verify_hostname(s): # hostname verification (disabled) t@@ -392,7 +392,7 @@ class Interface(threading.Thread): def run_tcp(self): try: #if self.use_ssl: self.s.do_handshake() - out = '' + message = '' while self.is_connected: try: timeout = False t@@ -417,18 +417,16 @@ class Interface(threading.Thread): self.send([('server.version', [ELECTRUM_VERSION, PROTOCOL_VERSION])], self.on_version) continue - out += msg + message += msg self.bytes_received += len(msg) if msg == '': self.is_connected = False while True: - s = out.find('\n') - if s==-1: break - c = out[0:s] - out = out[s+1:] - c = json.loads(c) - self.process_response(c) + response, message = parse_json(message) + if response is None: + break + self.process_response(response) except Exception: traceback.print_exc(file=sys.stdout) DIR diff --git a/lib/network_proxy.py b/lib/network_proxy.py t@@ -25,10 +25,10 @@ import traceback import json import Queue from network import Network -from util import print_error, print_stderr +from util import print_error, print_stderr, parse_json from simple_config import SimpleConfig -from daemon import parse_json, NetworkServer, DAEMON_PORT +from daemon import NetworkServer, DAEMON_PORT DIR diff --git a/lib/util.py b/lib/util.py t@@ -211,3 +211,15 @@ def raw_input(prompt=None): import __builtin__ builtin_raw_input = __builtin__.raw_input __builtin__.raw_input = raw_input + + + +def parse_json(message): + n = message.find('\n') + if n==-1: + return None, message + try: + j = json.loads( message[0:n] ) + except: + j = None + return j, message[n+1:]