Ejemplo n.º 1
0
 def __init__(self, sock, addr, server):
     self.sock = sock
     self.addr = addr
     self.server = server
     self.sock.setblocking(False)
     self.buf = []
     self.readEvent = libevent.createEvent(
         self.sock,libevent.EV_READ|libevent.EV_PERSIST, self._doRead)
     self.writeEvent = libevent.createEvent(
         self.sock,libevent.EV_WRITE, self._doWrite)
     self.startReading()
Ejemplo n.º 2
0
 def __init__(self, sock, addr, server):
     self.sock = sock
     self.addr = addr
     self.server = server
     self.sock.setblocking(False)
     self.buf = []
     self.readEvent = libevent.createEvent(
         self.sock, libevent.EV_READ | libevent.EV_PERSIST, self._doRead)
     self.writeEvent = libevent.createEvent(self.sock, libevent.EV_WRITE,
                                            self._doWrite)
     self.startReading()
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 5
0
 def listen(self):
     self.sock.bind((self.addr, self.port))
     self.sock.listen(5)
     events = libevent.EV_READ | libevent.EV_PERSIST
     libevent.createEvent(self.sock, events, self._callback).addToLoop()
Ejemplo n.º 6
0
def makeEvent(fd=0, events=libevent.EV_WRITE):
    return libevent.createEvent(fd, events, passThroughEventCallback)
Ejemplo n.º 7
0
def makeEvent(fd=0, events=libevent.EV_WRITE):
    return libevent.createEvent(fd, events, passThroughEventCallback)	
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
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()
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
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()
Ejemplo n.º 12
0
 def listen(self):
     self.sock.bind((self.addr, self.port))
     self.sock.listen(5)
     events = libevent.EV_READ|libevent.EV_PERSIST
     libevent.createEvent(self.sock, events, self._callback).addToLoop()