From f52981cc5d941027238771cf94d8dda06a06ddef Mon Sep 17 00:00:00 2001 From: genjix Date: Wed, 21 Mar 2012 10:47:00 +0000 Subject: [PATCH] Return JSONs on error scenarios instead of polluting stdout :) --- stratum.py | 20 ++++++++++++-------- 1 files changed, 12 insertions(+), 8 deletions(-) diff --git a/stratum.py b/stratum.py index 8a25065..dcc32b2 100644 --- a/stratum.py +++ b/stratum.py @@ -72,7 +72,7 @@ class Session: def pop_request(self): return self.request_queue.get() - def push_response(self): + def push_response(self, item): self.response_queue.put(item) def pop_response(self): @@ -136,16 +136,17 @@ class TcpClientRequestor(threading.Thread): if raw_buffer == -1: return True - command = self.message[0:raw_buffer].strip() + raw_command = self.message[0:raw_buffer].strip() self.message = self.message[raw_buffer + 1:] - if command == 'quit': + if raw_command == 'quit': return False try: - command = json.loads(command) + command = json.loads(raw_command) except: - print "json error", repr(command) - continue + self.session.push_response( + {"error": "bad JSON", "request": raw_command}) + return True try: # Try to load vital fields, and return an error if @@ -153,11 +154,14 @@ class TcpClientRequestor(threading.Thread): message_id = command['id'] method = command['method'] except KeyError: - # This should return an error JSON in response. - print "syntax error", repr(command), self.session.address[0] + # Return an error JSON in response. + self.session.push_response( + {"error": "syntax error", "request": raw_command}) else: self.session.push_request(command) + return True + class TcpServer(threading.Thread): def __init__(self, shared, processor): -- 1.7.1