3
from twisted.internet import protocol
4
from twisted.protocols import basic
6
class Error(Exception):
9
class JSONParseError(Error):
12
class JSONStream(object):
14
def parse(self, buffer):
17
simplejson.loads(buffer)
18
except JSONParseError, e:
19
raise JSONParseError, str(e)
21
class JSONLineReceiver(basic.LineReceiver):
24
basic.LineReceiver.__init__(self)
27
def connectionMade(self):
28
self.stream = JSONStream()
30
def rawDataReceived(self, data):
32
self.stream.parse(data)
33
except JSONParseError:
34
self.transport.loseConnection()
36
def connectionLost(self, reason):
39
class JSONRawReceiverFactory(protocol.Protocol)
41
def buildProtocol(self, addr):
42
p = JSONLineReceiver()