import json
import threading
import socket
import SocketServer

from orderbook import asks, bids


class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        try:
            while True:
                data = self.request.recv(1024)
                response = ''
                if data:
                    response += handle_data(data)
                    cur_thread = threading.current_thread()
                    response += "\n{}: {}".format(cur_thread.name, data)
                self.request.sendall(response)
        except socket.error:
            # Surpress errno 13 Broken Pipe
            pass


class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass


def create_server(host="localhost", port=0):
    server = ThreadedTCPServer((host, port), ThreadedTCPRequestHandler)
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    return server


def handle_data(data):
    data = json.loads(data)
    if data['type'] == 'ask':
        handle_ask(data)
    elif data['type'] == 'bid':
        handle_bid(data)
    elif data['type'] == 'greeting':
        handle_greeting(data)


def handle_ask(ask):
    asks.append(ask)


def handle_bid(bid):
    bids.append(bid)


def handle_greeting(greeting):
    pass

@@ -295,16 +295,17 @@
 se = ''%0A
+%0A
         
@@ -321,16 +321,110 @@
 f data:%0A
+                    handled_data = handle_data(data)%0A                    if handled_data:%0A    
         
@@ -447,28 +447,28 @@
 onse += 
+str(
 handle
+d
 _
-data(
 data)%0A  
@@ -594,24 +594,25 @@
 name, data)%0A
+%0A
             
@@ -645,16 +645,148 @@
 esponse)
+%0A%0A                if data and handled_data:%0A                    if handled_data%5B'type'%5D == 'greeting':%0A                        break
 %0A       
@@ -1243,16 +1243,29 @@
 (data):%0A
+    try:%0A    
     data
@@ -1280,24 +1280,28 @@
 loads(data)%0A
+    
     if data%5B
@@ -1322,24 +1322,35 @@
 k':%0A        
+    return 
 handle_ask(d
@@ -1350,24 +1350,28 @@
 e_ask(data)%0A
+    
     elif dat
@@ -1394,24 +1394,35 @@
 d':%0A        
+  
+  return 
 handle_bid(d
@@ -1422,24 +1422,28 @@
 e_bid(data)%0A
+    
     elif dat
@@ -1471,24 +1471,35 @@
 g':%0A        
+    
+return 
 handle_greet
@@ -1508,16 +1508,91 @@
 g(data)%0A
+    except ValueError, e:%0A        print e.message%0A        return e.message%0A
 %0A%0Adef ha
@@ -1704,17 +1704,28 @@
 eting):%0A    
-pass
+return greeting
 %0A

Add return statements.