import json
import threading
import socket
import SocketServer

from orderbook import match_bid, offers, bids

messages = []


class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        try:
            while True:
                data = self.request.recv(1024)
                if data:
                    data = json.loads(data)
                    messages.append(data)
                    print "MESSAGES: {}".format(messages)
                    if data['type'] == 'bid':
                        response = handle_bid(data)
                    elif data['type'] == 'offer':
                        response = handle_offer(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_offer(offer):
    offers.append(offer)

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

import threading
import socket
import SocketServer

messages = []


class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        try:
            while True:
                data = self.request.recv(1024)
                if data:
                    messages.append(data)
                    print "MESSAGES: {}".format(messages)
                cur_thread = threading.current_thread()
                response = "{}: {}".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

--- 
+++ 
@@ -1,6 +1,9 @@
+import json
 import threading
 import socket
 import SocketServer
+
+from orderbook import match_bid, offers, bids
 
 messages = []
 
@@ -12,10 +15,15 @@
             while True:
                 data = self.request.recv(1024)
                 if data:
+                    data = json.loads(data)
                     messages.append(data)
                     print "MESSAGES: {}".format(messages)
+                    if data['type'] == 'bid':
+                        response = handle_bid(data)
+                    elif data['type'] == 'offer':
+                        response = handle_offer(data)
                 cur_thread = threading.current_thread()
-                response = "{}: {}".format(cur_thread.name, data)
+                response = "\n{}: {}".format(cur_thread.name, data)
                 self.request.sendall(response)
         except socket.error:
             # Surpress errno 13 Broken Pipe
@@ -32,3 +40,10 @@
     server_thread.daemon = True
     server_thread.start()
     return server
+
+
+def handle_offer(offer):
+    offers.append(offer)
+
+def handle_bid(bid):
+    bids.append(bid)
Add basic handling of incoming bids and offers.
