Пример #1
0
 def testSignalHandler(self):
     signalHandlerCallback = lambda signum, events, obj: obj.removeFromLoop()
     signalHandler = libevent.createSignalHandler(signal.SIGUSR1, signalHandlerCallback)
     signalHandler.addToLoop()
     signalSenderCallback = lambda fd, events, obj: os.kill(os.getpid(), signal.SIGUSR1)
     timer = libevent.createTimer(signalSenderCallback)
     timer.addToLoop(1)
     libevent.dispatch()
Пример #2
0
 def testSignalHandler(self):
     signalHandlerCallback = lambda signum, events, obj: obj.removeFromLoop()
     signalHandler = libevent.createSignalHandler(signal.SIGUSR1, signalHandlerCallback)
     signalHandler.addToLoop()
     signalSenderCallback = lambda fd, events, obj: os.kill(os.getpid(), signal.SIGUSR1)
     timer = libevent.createTimer(signalSenderCallback)
     timer.addToLoop(1)
     libevent.dispatch()
Пример #3
0
    def testSimpleSocketCallback(self):
        def serverCallback(fd, events, eventObj):
            s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
            client, addr = s.accept()
            client.send("foo")
            eventObj.removeFromLoop()
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setblocking(False)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(("127.0.0.1", 50505))
        s.listen(5)
        serverEvent = libevent.createEvent(fd=s, events=libevent.EV_READ, callback=serverCallback)
        serverEvent.addToLoop()
        c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        c.setblocking(False)
        c.connect_ex(("127.0.0.1", 50505))

        libevent.dispatch()

        c.setblocking(True)
        self.assertEqual(c.recv(3), "foo")
        c.close()
        s.close()
Пример #4
0
    def testSimpleSocketCallback(self):
        def serverCallback(fd, events, eventObj):
            s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
            client, addr = s.accept()
            client.send("foo")
            eventObj.removeFromLoop()

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setblocking(False)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(("127.0.0.1", 50505))
        s.listen(5)
        serverEvent = libevent.createEvent(fd=s, events=libevent.EV_READ, callback=serverCallback)
        serverEvent.addToLoop()
        c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        c.setblocking(False)
        c.connect_ex(("127.0.0.1", 50505))

        libevent.dispatch()

        c.setblocking(True)
        self.assertEqual(c.recv(3), "foo")
        c.close()
        s.close()
Пример #5
0
def main():
    libevent.createSignalHandler(signal.SIGINT, handleSigInt).addToLoop()
    echosrv = EchoServer()
    libevent.dispatch()
Пример #6
0
 def testLoopExit(self):
     cb = lambda fd, events, obj: libevent.loopExit(0)
     timer = libevent.createTimer(cb)
     timer.addToLoop(timeout=2)
     libevent.dispatch()
Пример #7
0
 def testLoopExit(self):
     cb = lambda fd, events, obj: libevent.loopExit(0)
     timer = libevent.createTimer(cb)
     timer.addToLoop(timeout=2)
     libevent.dispatch()
Пример #8
0
        self.sock = sock
        self.addr = addr
        self.sock.setblocking(False)
        libevent.createEvent(sock, libevent.EV_READ,
                             self.callback_onread).addToLoop()

    def callback_onread(self, fd, events, event_obj):
        buf = self.sock.recv(4096)
        if not buf:  # just disconnect
            self.sock.close()
            return
        # Yeah, print!
        print "%r %r" % (self.addr, buf)
        self.sock.send("finished")
        # reuse current event
        event_obj.addToLoop()


if __name__ == '__main__':
    # bind.
    sock_srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_srv.setblocking(False)
    sock_srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock_srv.bind(('localhost', 50000))
    sock_srv.listen(5)

    libevent.createSignalHandler(signal.SIGINT, callback_interrupt).addToLoop()
    libevent.createEvent(sock_srv, libevent.EV_READ | libevent.EV_PERSIST,
                         callback_onconnect).addToLoop()
    libevent.dispatch()
Пример #9
0
class Connection:
    def __init__(self, sock, addr):
        self.sock = sock
        self.addr = addr
        self.sock.setblocking(False)
        libevent.createEvent(sock, libevent.EV_READ, self.callback_onread).addToLoop()
    
    def callback_onread(self, fd, events, event_obj):
        buf = self.sock.recv(4096)
        if not buf: # just disconnect
            self.sock.close()
            return
        # Yeah, print!
        print "%r %r" % (self.addr, buf)
        self.sock.send("finished")
        # reuse current event
        event_obj.addToLoop()

if __name__ == '__main__':
    # bind.
    sock_srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_srv.setblocking(False)
    sock_srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock_srv.bind(('localhost', 50000))
    sock_srv.listen(5)
    
    libevent.createSignalHandler(signal.SIGINT, callback_interrupt).addToLoop()
    libevent.createEvent(sock_srv, libevent.EV_READ|libevent.EV_PERSIST, callback_onconnect).addToLoop()
    libevent.dispatch()
Пример #10
0
def main():
    libevent.createSignalHandler(signal.SIGINT, handleSigInt).addToLoop()
    echosrv = EchoServer()
    libevent.dispatch()