Reputation: 26487
I've got a following code developed using python twisted library:
class Cache(protocol.Protocol):
def __init__(self, factory):
self.factory = factory
def dataReceived(self, data):
request = json.loads(data)
self.factory.handle[request['command']](**request)
self.transport.write(data)
class CacheFactory(protocol.Factory):
def buildProtocol(self, addr):
return Cache(self)
def handle_get(self, **kwargs):
print 'get\n', kwargs
def handle_set(self, **kwargs):
print 'set\n', kwargs
def handle_delete(self, **kwargs):
print 'delete\n', kwargs
handle = {
'get': handle_get,
'set': handle_set,
'delete': handle_delete,
}
reactor.listenTCP(int(sys.argv[1]), CacheFactory())
reactor.run()
I run a client connection using telnet:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
{"command": "set", "value": 1234567890}
Connection closed by foreign host.
an exception is thrown:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 460, in doRead
rval = self.protocol.dataReceived(data)
File "./server.py", line 18, in dataReceived
self.factory.handle[request['command']](**request)
exceptions.TypeError: handle_set() takes exactly 1 argument (0 given)
which I don't understand. There could be something wrong with line self.factory.handle[request['command']](**request)
, but in my opinion it's correct - it passes the self
parameter implicitly (it's a method afterall) and unpacked request parameter explicitly. The exception message says the function takes 1 argument, and it's a lie :) because it takes 2 parameters: self, **kwargs
. And it's not true that I pass 0 arguments, since I pass 2.
Can someone help me to spot the problem?
in case it helps, the json request is decoded into:
{u'command': u'set', u'value': 1234567890}
Upvotes: 2
Views: 2523
Reputation: 229601
As it is now, the handle_*
methods are instance methods, yet the handle
dict points to unbound methods. That is, self
is not being passed implicitly. Try this instead:
class CacheFactory(protocol.Factory):
def buildProtocol(self, addr):
return Cache(self)
def handle_get(self, **kwargs):
print 'get\n', kwargs
def handle_set(self, **kwargs):
print 'set\n', kwargs
def handle_delete(self, **kwargs):
print 'delete\n', kwargs
def __init__(self, *args, **kwargs):
protocol.Factory.__init__(self, *args, **kwargs)
self.handle = {
'get': self.handle_get,
'set': self.handle_set,
'delete': self.handle_delete,
}
Alternatively, you could keep your handle
the same and do this:
def dataReceived(self, data):
request = json.loads(data)
self.factory.handle[request['command']](self.factory, **request)
self.transport.write(data)
Or, you could take this approach and then you don't need a handle
dict either way:
def dataReceived(self, data):
request = json.loads(data)
getattr(self.factory, "handle_%s" % (request['command'],))(**request)
self.transport.write(data)
Also note your dataReceived
, as it is now, is unsafe, because packets might get split up arbitrarily - that is, you might not receive an entire json
message in one shot.
Upvotes: 4