electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 9efc25bdba3d56d13dfc79975cf228afcec85e00
parent 9a07c1cb446fc8aad4205b0e8a34a995bdefb4da
Author: ThomasV <thomasv@gitorious>
Date:   Sat, 26 Jul 2014 16:24:22 +0200

interface: use parse_json

Diffstat:
Mlib/daemon.py | 11+----------
Mlib/interface.py | 16+++++++---------
Mlib/network_proxy.py | 4++--
Mlib/util.py | 12++++++++++++
4 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/lib/daemon.py b/lib/daemon.py @@ -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 @@ -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:] diff --git a/lib/interface.py b/lib/interface.py @@ -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) @@ -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 @@ -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) diff --git a/lib/network_proxy.py b/lib/network_proxy.py @@ -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 diff --git a/lib/util.py b/lib/util.py @@ -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:]