import json

class Server:

    def __init__(self, infile, outfile):
        self.infile = infile
        self.outfile = outfile

    def start(self):
        while True:
            line = self.infile.readline()
            if not line:
                break # stop at EOF
            try:
                request = json.loads(line)
            except ValueError:
                self.send({"error": "could not parse json"})
                continue
            response = {} # dummy response
            self.send(response)

    def send(self, message):
        self.outfile.write(json.dumps(message, self.outfile) + '\n')
        self.outfile.flush()

@@ -6,16 +6,82 @@
 t json%0A%0A
+class StopServer(Exception):%0A    %22%22%22Raise to stop the server.%22%22%22%0A%0A
 class Se
@@ -210,16 +210,33 @@
 (self):%0A
+        try:%0A    
         
@@ -259,16 +259,204 @@
         
+  
+  request = self.recv()%0A                response = self.handle_json(request)%0A                self.send(response)%0A        except StopServer:%0A            pass%0A%0A    def recv(self):%0A        
 line = s
@@ -485,20 +485,16 @@
         
-   
- 
 if not l
@@ -514,17 +514,24 @@
     
-    break
+raise StopServer
  # s
@@ -545,37 +545,192 @@
 EOF%0A        
-    try
+return line%0A%0A    def send(self, response):%0A        self.outfile.write(response + '%5Cn')%0A        self.outfile.flush()%0A%0A    def handle_json(self, json_line)
 :%0A    
+    try:%0A
             
@@ -754,18 +754,19 @@
 ads(
+json_
 line)%0A
-    
     
@@ -800,29 +800,33 @@
         
-  
-  self.send
+return json.dumps
 (%7B%22error
@@ -865,224 +865,124 @@
     
-        continue%0A            response = %7B%7D # dummy response%0A            self.send(response)%0A%0A    def send(self, message):%0A        self.outfile.write(json.dumps(message, self.outfile) + '%5Cn')%0A        self.outfile.flush()
+response = self._handle(request)%0A        return json.dumps(response)%0A%0A    def _handle(self, request):%0A        return %7B%7D
 %0A

Reorganize server functions