def serve(port):
    s = coro.tcp_sock()
    s.bind(("", port))
    s.listen(5)
    while 1:
        conn, addr = s.accept()
        coro.spawn(go, conn, addr)
Exemple #2
0
def serve (port=9000):
    s = coro.tcp_sock()
    s.bind (('', port))
    s.listen (50)
    while 1:
        conn, addr = s.accept()
        coro.spawn (session, conn, addr)
Exemple #3
0
    def start (self, addr, retries=5):
        """Start the web server listening on addr in a new coroutine.

        Try up to <retries> time to bind to that address.
        Raises an exception if the bind fails."""

        self.sock = coro.tcp_sock()
        self.sock.set_reuse_addr()
        done = 0
        save_errno = 0
        self.addr = addr
        while not done:
            for x in xrange (retries):
                try:
                    self.sock.bind (addr)
                except OSError, why:
                    if why.errno not in (errno.EADDRNOTAVAIL, errno.EADDRINUSE):
                        raise
                    else:
                        save_errno = 0
                        if why.errno == errno.EADDRINUSE:
                            was_eaddrinuse = 1
                else:
                    done = 1
                    break
                coro.sleep_relative (1)
            else:
                self.log ('cannot bind to %s:%d after 5 attempts, errno = %d' % (addr[0], addr[1], save_errno))
                coro.sleep_relative (15)
Exemple #4
0
 def __init__ (self, addr='127.0.0.1', port=BITCOIN_PORT):
     self.addr = addr
     self.port = port
     self.nonce = make_nonce()
     self.conn = coro.tcp_sock()
     self.packet_count = 0
     self.stream = coro.read_stream.sock_stream (self.conn)
Exemple #5
0
def serve(port=9000):
    s = coro.tcp_sock()
    s.bind(('', port))
    s.listen(50)
    while 1:
        conn, addr = s.accept()
        coro.spawn(session, conn, addr)
Exemple #6
0
 def create_sock(self):
     # the assumption here is that you would never run an HTTP server
     #   on a unix socket, if you need that then override this method.
     if ':' in self.addr[0]:
         return coro.tcp6_sock()
     else:
         return coro.tcp_sock()
Exemple #7
0
def serve(port):
    s = coro.tcp_sock()
    s.bind(('', port))
    s.listen(5)
    while 1:
        conn, addr = s.accept()
        coro.spawn(go, conn, addr)
Exemple #8
0
 def __init__(self, addr='127.0.0.1', port=BITCOIN_PORT):
     self.addr = addr
     self.port = port
     self.nonce = make_nonce()
     self.conn = coro.tcp_sock()
     self.packet_count = 0
     self.stream = coro.read_stream.sock_stream(self.conn)
Exemple #9
0
 def create_sock (self):
     # the assumption here is that you would never run an HTTP server
     #   on a unix socket, if you need that then override this method.
     if ':' in self.addr[0]:
         return coro.tcp6_sock()
     else:
         return coro.tcp_sock()
Exemple #10
0
def serve(saddr):
    ip, port = saddr
    s = coro.tcp_sock()
    s.bind(('0.0.0.0', port + 9000))
    s.listen(5)
    while 1:
        conn, caddr = s.accept()
        coro.spawn(session, conn, caddr, saddr)
Exemple #11
0
def serve():
    s = coro.tcp_sock()
    s.bind (('', 9001))
    s.listen (5)
    while 1:
        c, a = s.accept()
        t = terminal (c)
        the_arena.listeners.append (t)
 def _start_listener(self):
     s = coro.tcp_sock()
     s.bind(('127.0.0.1', 0))
     s.listen(5)
     addr = s.getsockname()
     self.port = addr[1]
     self._dummy_thread = coro.spawn(self._dummy_listener, s)
     coro.yield_slice()
Exemple #13
0
 def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
     """Constructor.  May be extended, do not override."""
     BaseServer.__init__(self, server_address, RequestHandlerClass)
     self.socket = coro.tcp_sock()
     self.socket.set_reuse_addr()
     if bind_and_activate:
         self.server_bind()
         self.server_activate()
Exemple #14
0
def serve (saddr):
    ip, port = saddr
    s = coro.tcp_sock()
    s.bind (('0.0.0.0', port + 9000))
    s.listen (5)
    while 1:
        conn, caddr = s.accept()
        coro.spawn (session, conn, caddr, saddr)
 def _start_listener(self):
     s = coro.tcp_sock()
     s.bind(('127.0.0.1', 0))
     s.listen(5)
     addr = s.getsockname()
     self.port = addr[1]
     self._dummy_thread = coro.spawn(self._dummy_listener, s)
     coro.yield_slice()
Exemple #16
0
 def serve(port):
     s = coro.tcp_sock()
     s.bind(("", port))
     s.listen(5)
     with self.assertRaises(coro.TimeoutError):
         conn, addr = coro.with_timeout(1, s.accept)
     # do this a second time to make sure no SimultaneousErrors occur
     with self.assertRaises(coro.TimeoutError):
         conn, addr = coro.with_timeout(1, s.accept)
Exemple #17
0
def serve():
    s = coro.tcp_sock()
    s.bind (('', 9001))
    s.listen (5)
    coro.write_stderr ('Try "telnet localhost 9001" to watch the worms!\n')
    while 1:
        c, a = s.accept()
        t = terminal (c)
        the_arena.listeners.append (t)
 def serve (port):
     s = coro.tcp_sock()
     s.bind (('', port))
     s.listen (5)
     with self.assertRaises(coro.TimeoutError):
         conn, addr = coro.with_timeout(1, s.accept)
     # do this a second time to make sure no SimultaneousErrors occur
     with self.assertRaises(coro.TimeoutError):
         conn, addr = coro.with_timeout(1, s.accept)
Exemple #19
0
 def __init__(self, conn, addr, saddr):
     self.conn = conn
     self.addr = addr
     self.saddr = saddr
     self.id = session.counter
     session.counter += 1
     self.proxy = coro.tcp_sock()
     self.proxy.connect(saddr)
     coro.spawn(self.feed, self.conn, self.proxy, '<==')
     coro.spawn(self.feed, self.proxy, self.conn, '==>')
 def _blocker_thread(self):
     self._blocker_socket = coro.tcp_sock()
     self._blocker_socket.connect(('127.0.0.1', self.port))
     while 1:
         coro.print_stderr('reading')
         try:
             self._blocker_socket.read(1024)
         except coro.ClosedError:
             coro.print_stderr('it was closed')
             return
 def _blocker_thread(self):
     self._blocker_socket = coro.tcp_sock()
     self._blocker_socket.connect(('127.0.0.1', self.port))
     while 1:
         coro.print_stderr('reading')
         try:
             self._blocker_socket.read(1024)
         except coro.ClosedError:
             coro.print_stderr('it was closed')
             return
Exemple #22
0
 def __init__ (self, conn, addr, saddr):
     self.conn = conn
     self.addr = addr
     self.saddr = saddr
     self.id = session.counter
     session.counter += 1
     self.proxy = coro.tcp_sock()
     self.proxy.connect (saddr)
     coro.spawn (self.feed, self.conn, self.proxy, '<==')
     coro.spawn (self.feed, self.proxy, self.conn, '==>')
Exemple #23
0
 def __init__(self, addr):
     self.msgid = 1
     self.addr = addr
     if isinstance(addr, tuple):
         self.sock = coro.tcp_sock()
     else:
         self.sock = coro.unix_sock()
     self.sock.connect(addr)
     self.pending = {}
     self.recv_thread_ob = coro.spawn(self.recv_thread)
Exemple #24
0
 def __init__(self, addr):
     self.msgid = 1
     self.addr = addr
     if isinstance(addr, tuple):
         self.sock = coro.tcp_sock()
     else:
         self.sock = coro.unix_sock()
     self.sock.connect(addr)
     self.pending = {}
     self.recv_thread_ob = coro.spawn(self.recv_thread)
Exemple #25
0
    def connect(self):
        """Connect to the host and port specified in __init__."""
        self.sock = coro.tcp_sock()
        if self.timeout:
            coro.with_timeout(self.timeout, self.sock.connect, (self.host, self.port))
        else:
            self.sock.connect((self.host, self.port))

        if self._tunnel_host:
            self._tunnel()
Exemple #26
0
 def __init__ (self, host, port=80, conn=None, inflight=100):
     self.host = host
     self.inflight = coro.semaphore (inflight)
     if conn is None:
         self.conn = coro.tcp_sock()
         self.conn.connect ((host, port))
     else:
         self.conn = conn
     self.stream = coro.read_stream.sock_stream (self.conn)
     self.pending = coro.fifo()
     coro.spawn (self.read_thread)
Exemple #27
0
 def go (self):
     "Connect to the server.  Spawns a new thread to monitor the connection."
     if self.tls_ctx:
         self.s = coro.ssl.sock (self.tls_ctx)
     else:
         self.s = coro.tcp_sock()
     try:
         self.s.connect ((self.host, self.port))
     except:
         LOG.exc()
     self.s.send ('AMQP' + struct.pack ('>BBBB', *self.version))
     self.buffer = self.s.recv (self.buffer_size)
     if self.buffer.startswith ('AMQP'):
         # server rejection
         raise AMQPError (
             "version mismatch: server wants %r" % (
                 struct.unpack ('>4B', self.buffer[4:8])
             )
         )
     else:
         coro.spawn (self.recv_loop)
         # pull the first frame off (should be connection.start)
         ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.start')
         # W ('connection start\n')
         # dump_ob (frame)
         mechanisms = frame.mechanisms.split()
         self.server_properties = frame.server_properties
         if 'PLAIN' in mechanisms:
             response = '\x00%s\x00%s' % self.auth
         else:
             raise AuthenticationError ("no shared auth mechanism: %r" % (mechanisms,))
         reply = spec.connection.start_ok (
             # XXX put real stuff in here...
             client_properties=self.properties,
             response=response
         )
         self.send_frame (spec.FRAME_METHOD, 0, reply)
         # XXX
         # XXX according to the 'grammar' from the spec, we might get a 'challenge' frame here.
         # XXX
         ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.tune')
         self.tune = frame
         # I'm AMQP, and I approve this tune value.
         self.send_frame (
             spec.FRAME_METHOD, 0,
             spec.connection.tune_ok (frame.channel_max, frame.frame_max, self.heartbeat)
         )
         # ok, ready to 'open' the connection.
         self.send_frame (
             spec.FRAME_METHOD, 0,
             spec.connection.open (self.virtual_host)
         )
         ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.open_ok')
    def connect_socket(self):
        """Connect to database at self.address.

        Returns socket object and function to send data on the socket.
        Works with TCP and Unix-domain sockets."""

        if isinstance(self.address, type('')):
            sock = coro.unix_sock()
        else:
            sock = coro.tcp_sock()
        sock.connect (self.address)
        return sock
 def __init__(self, ip=None, port=22, bind_ip=None, hostname=None, sock=None):
     self.ip = ip
     self.port = port
     self.bind_ip = bind_ip
     self.hostname = hostname
     if sock is None:
         if ':' in ip:
             self.s = coro.tcp6_sock()
         else:
             self.s = coro.tcp_sock()
     else:
         self.s = sock
         self.peer = self.s.getpeername()
Exemple #30
0
 def go(self):
     "Connect to the server.  Spawns a new thread to monitor the connection."
     if self.tls_ctx:
         self.s = coro.ssl.sock(self.tls_ctx)
     else:
         self.s = coro.tcp_sock()
     try:
         self.s.connect((self.host, self.port))
     except:
         LOG.exc()
     self.s.send('AMQP' + struct.pack('>BBBB', *self.version))
     self.buffer = self.s.recv(self.buffer_size)
     if self.buffer.startswith('AMQP'):
         # server rejection
         raise AMQPError("version mismatch: server wants %r" %
                         (struct.unpack('>4B', self.buffer[4:8])))
     else:
         coro.spawn(self.recv_loop)
         # pull the first frame off (should be connection.start)
         ftype, channel, frame = self.expect_frame(spec.FRAME_METHOD,
                                                   'connection.start')
         # W ('connection start\n')
         # dump_ob (frame)
         mechanisms = frame.mechanisms.split()
         self.server_properties = frame.server_properties
         if 'PLAIN' in mechanisms:
             response = '\x00%s\x00%s' % self.auth
         else:
             raise AuthenticationError("no shared auth mechanism: %r" %
                                       (mechanisms, ))
         reply = spec.connection.start_ok(
             # XXX put real stuff in here...
             client_properties=self.properties,
             response=response)
         self.send_frame(spec.FRAME_METHOD, 0, reply)
         # XXX
         # XXX according to the 'grammar' from the spec, we might get a 'challenge' frame here.
         # XXX
         ftype, channel, frame = self.expect_frame(spec.FRAME_METHOD,
                                                   'connection.tune')
         self.tune = frame
         # I'm AMQP, and I approve this tune value.
         self.send_frame(
             spec.FRAME_METHOD, 0,
             spec.connection.tune_ok(frame.channel_max, frame.frame_max,
                                     self.heartbeat))
         # ok, ready to 'open' the connection.
         self.send_frame(spec.FRAME_METHOD, 0,
                         spec.connection.open(self.virtual_host))
         ftype, channel, frame = self.expect_frame(spec.FRAME_METHOD,
                                                   'connection.open_ok')
Exemple #31
0
def serve (addr):
    addr0 = parse_addr_arg (addr)
    if ':' in addr0[0]:
        ipv6_server_addrs.append (addr0)
        s = coro.tcp6_sock()
    else:
        ipv4_server_addrs.append (addr0)
        s = coro.tcp_sock()
    s.bind (addr0)
    s.listen (100)
    LOG ('starting', addr0)
    while 1:
        conn, addr1 = s.accept()
        G.in_conn_sem.acquire (1)
        Connection (addr0, addr1, sock=conn)
Exemple #32
0
def serve(addr):
    addr0 = parse_addr_arg(addr)
    if ':' in addr0[0]:
        ipv6_server_addrs.append(addr0)
        s = coro.tcp6_sock()
    else:
        ipv4_server_addrs.append(addr0)
        s = coro.tcp_sock()
    s.bind(addr0)
    s.listen(100)
    LOG('starting', addr0)
    while 1:
        conn, addr1 = s.accept()
        G.in_conn_sem.acquire(1)
        Connection(addr0, addr1, sock=conn)
Exemple #33
0
 def __init__(self, my_addr, other_addr, conn=None):
     self.my_addr = my_addr
     self.other_addr = other_addr
     self.nonce = make_nonce()
     self.other_version = None
     self.send_mutex = coro.mutex()
     if conn is None:
         if ':' in other_addr[0]:
             self.conn = coro.tcp6_sock()
         else:
             self.conn = coro.tcp_sock()
     else:
         self.conn = conn
     self.packet_count = 0
     self.stream = coro.read_stream.sock_stream(self.conn)
Exemple #34
0
 def __init__ (self, my_addr, other_addr, conn=None):
     self.my_addr = my_addr
     self.other_addr = other_addr
     self.nonce = make_nonce()
     self.other_version = None
     self.send_mutex = coro.mutex()
     if conn is None:
         if ':' in other_addr[0]:
             self.conn = coro.tcp6_sock()
         else:
             self.conn = coro.tcp_sock()
     else:
         self.conn = conn
     self.packet_count = 0
     self.stream = coro.read_stream.sock_stream (self.conn)
Exemple #35
0
def client (ip='127.0.0.1', port=9000):
    global alive
    alive += 1
    try:
        s = coro.tcp_sock()
        s.connect ((ip, port))
        for i in range (10):
            s.send ('howdy there\r\n')
            assert (s.recv_exact (13) == 'howdy there\r\n')
        coro.write_stderr ('.')
        s.close()
    finally:
        alive -= 1
        if alive == 0:
            coro.write_stderr ('\ndone.\n')
            coro.set_exit()
 def __init__(self,
              ip=None,
              port=22,
              bind_ip=None,
              hostname=None,
              sock=None):
     self.ip = ip
     self.port = port
     self.bind_ip = bind_ip
     self.hostname = hostname
     if sock is None:
         if ':' in ip:
             self.s = coro.tcp6_sock()
         else:
             self.s = coro.tcp_sock()
     else:
         self.s = sock
         self.peer = self.s.getpeername()
Exemple #37
0
 def __init__ (self, my_addr, other_addr, conn=None, log_fun=None, verbose=False, packet=False):
     self.log_fun = log_fun
     self.verbose = verbose
     self.packet = packet
     self.my_addr = my_addr
     self.other_addr = other_addr
     self.nonce = make_nonce()
     self.other_version = None
     self.send_mutex = coro.mutex()
     if conn is None:
         if ':' in other_addr[0]:
             self.conn = coro.tcp6_sock()
         else:
             self.conn = coro.tcp_sock()
     else:
         self.conn = conn
     self.packet_count = 0
     coro.spawn (self.go)
    def test_fired(self):
        s = coro.tcp_sock()
        s.connect(("127.0.0.1", self.port))
        self._fired_blocker_socket = s
        # We need to somehow schedule two threads to both wake up on kevent at
        # the same time in a particular order.  The first one will call close
        # on the socket of the second one.
        f = open("test_fire", "w")
        coro.set_handler((f.fileno(), coro.EVFILT.VNODE), self._fired_closer, fflags=coro.NOTE.DELETE)

        t2 = coro.spawn(self._fired_blocker)
        # t2.set_max_selfish_acts(1)
        # Yield to allow fired blocker to block.
        coro.yield_slice()
        # Now, cause threads blocked on kevents to get scheduled in a specific
        # order.
        os.unlink("test_fire")
        s.send("force send")
        # Let those threads run.
        coro.yield_slice()
    def test_fired(self):
        s = coro.tcp_sock()
        s.connect(('127.0.0.1', self.port))
        self._fired_blocker_socket = s
        # We need to somehow schedule two threads to both wake up on kevent at
        # the same time in a particular order.  The first one will call close
        # on the socket of the second one.
        f = open('test_fire', 'w')
        coro.set_handler((f.fileno(), coro.EVFILT.VNODE),
                         self._fired_closer,
                         fflags=coro.NOTE.DELETE)

        t2 = coro.spawn(self._fired_blocker)
        #t2.set_max_selfish_acts(1)
        # Yield to allow fired blocker to block.
        coro.yield_slice()
        # Now, cause threads blocked on kevents to get scheduled in a specific
        # order.
        os.unlink('test_fire')
        s.send('force send')
        # Let those threads run.
        coro.yield_slice()
Exemple #40
0
 def __init__(self,
              my_addr,
              other_addr,
              conn=None,
              log_fun=None,
              verbose=False,
              packet=False):
     self.log_fun = log_fun
     self.verbose = verbose
     self.packet = packet
     self.my_addr = my_addr
     self.other_addr = other_addr
     self.nonce = make_nonce()
     self.other_version = None
     self.send_mutex = coro.mutex()
     if conn is None:
         if ':' in other_addr[0]:
             self.conn = coro.tcp6_sock()
         else:
             self.conn = coro.tcp_sock()
     else:
         self.conn = conn
     self.packet_count = 0
     coro.spawn(self.go)
Exemple #41
0
 def make_socket(self):
     if isinstance(self.addr, str):
         return coro.unix_sock()
     else:
         return coro.tcp_sock()
Exemple #42
0
 def test_coro_main_connect(self):
     s = coro.tcp_sock()
     with self.assertRaises(coro.YieldFromMain):
         s.connect(('127.0.0.1', 80))
Exemple #43
0
 def make_socket (self):
     if isinstance(self.addr, str):
         return coro.unix_sock()
     else:
         return coro.tcp_sock()
Exemple #44
0
 def test_coro_main_yield(self):
     s = coro.tcp_sock()
     with self.assertRaises(coro.YieldFromMain):
         coro.yield_slice()
Exemple #45
0
 def test_coro_main_connect(self):
     s = coro.tcp_sock()
     with self.assertRaises(coro.YieldFromMain):
         s.connect (('127.0.0.1', 80))
Exemple #46
0
 def test_coro_main_yield(self):
     s = coro.tcp_sock()
     with self.assertRaises(coro.YieldFromMain):
         coro.yield_slice()
Exemple #47
0
    def go (self):
        "Connect to the server.  Spawns a new thread to monitor the connection."
        self.s = coro.tcp_sock()
        self.s.connect ((self.host, self.port))
        self._s_send_sema.acquire(1)
        try:
            self.s.send ('AMQP' + struct.pack ('>BBBB', *self.version))
        finally:
            self._s_send_sema.release(1)
        self.buffer = self.s.recv (self.buffer_size)
        if self.buffer.startswith ('AMQP'):
            # server rejection
            raise ProtocolError (
                "version mismatch: server wants %r" % (
                    struct.unpack ('>4B', self.buffer[4:8])
                    )
                )
        else:
            self._recv_loop_thread = coro.spawn (self.recv_loop)
            # pull the first frame off (should be connection.start)
            ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.start')
            #W ('connection start\n')
            #dump_ob (frame)
            mechanisms = frame.mechanisms.split()
            self.server_properties = frame.server_properties

            if self.consumer_cancel_notify is None:
                # adjsut to the server properties
                if self.server_properties['capabilities'].get('consumer_cancel_notify'):
                    self.consumer_cancel_notify = True
                    self.properties['capabilities']['consumer_cancel_notify'] = True

            else:
                if self.consumer_cancel_notify:
                    if not self.server_properties['capabilities'].get('consumer_cancel_notify'):
                        raise ProtocolError ('server capabilities says NO to consumer_cancel_notify')
                    self.properties['capabilities']['consumer_cancel_notify'] = True


            if 'PLAIN' in mechanisms:
                response = '\x00%s\x00%s' % self.auth
            else:
                raise AuthenticationError ("no shared auth mechanism: %r" % (mechanisms,))
            reply = spec.connection.start_ok (
                # XXX put real stuff in here...
                client_properties=self.properties,
                response=response
                )
            self.send_frame (spec.FRAME_METHOD, 0, reply)
            # XXX
            # XXX according to the 'grammar' from the spec, we might get a 'challenge' frame here.
            # XXX
            ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.tune')
            self.tune = frame
            if self.frame_max:
                self.tune.frame_max = min(self.tune.frame_max, self.frame_max)
            # I'm AMQP, and I approve this tune value.
            self.send_frame (
                spec.FRAME_METHOD, 0,
                spec.connection.tune_ok (frame.channel_max, frame.frame_max, self.heartbeat)
                )
            # ok, ready to 'open' the connection.
            self.send_frame (
                spec.FRAME_METHOD, 0,
                spec.connection.open (self.virtual_host)
                )
            ftype, channel, frame = self.expect_frame (spec.FRAME_METHOD, 'connection.open_ok')