return func
return realhook
+ def bind(self, bindto):
+ """Used as a decorator on a class which implements getdata and parse methods.
+ See modules/sockets.py for an example.
+ Takes an arg like:
+ [unix:]/foo/bar
+ [udp|tcp:][ip:]port
+ """
+ if len(bindto) == 0:
+ raise Exception('bindto must have a value')
+ if bindto[0] == '/':
+ return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, bindto)
+ if len(bindto) > 5 and bindto[0:5] == 'unix:':
+ return self._hooksocket(socket.AF_UNIX, socket.SOCK_STREAM, bindto[5:])
+ af = socket.AF_INET
+ ty = socket.SOCK_STREAM
+ host = '0.0.0.0'
+ if len(bindto) > 4 and bindto[0:4] == 'udp:':
+ ty = socket.SOCK_DGRAM
+ bindto = bindto[4:]
+ if len(bindto) > 4 and bindto[0:4] == 'tcp:':
+ bindto = bindto[4:]
+ print(repr(bindto), ':' in bindto)
+ if ':' in bindto:
+ print(bindto)
+ pieces = bindto.rsplit(':', 1)
+ host = pieces[0]
+ bindto = pieces[1]
+ print(pieces,host,bindto)
+ port = int(bindto)
+ return self._hooksocket(af, ty, (host, port))
+
def bind_tcp(self, host, port):
return self._hooksocket(socket.AF_INET, socket.SOCK_STREAM, (host, port))
def bind_udp(self, host, port):
self.sockets.append((sock,obj))
sock.listen(5)
self.parent.newfd(obj, sock.fileno())
- self.parent.log(repr(obj), '?', 'Socket ready to accept new connections')
+ self.parent.log(repr(obj), '?', 'Socket ready to accept new connections (%r, %r, %r, %r)' % (af, ty, address, cls))
def _destroy_socket(self, sock, obj):
obj.close()
def _make_closer(self, obj, client):
def close():
- print(repr(self), repr(obj))
- self.lib.parent.log(repr(self), '?', 'Closing child socket %d' % (client.fileno()))
+ self.lib.parent.log(repr(self), '?', 'Closing child socket #%d' % (client.fileno()))
try:
obj.closing()
except AttributeError:
client, addr = self.sock.accept()
obj = self.cls(client)
obj.close = self._make_closer(obj, client)
- self.lib.parent.log(repr(self), '?', 'New connection %d from %s' % (client.fileno(), addr))
+ self.lib.parent.log(repr(self), '?', 'New connection #%d from %s' % (client.fileno(), addr))
self.clients.append((client,obj))
self.lib.parent.newfd(obj, client.fileno())
return []