Ejemplo n.º 1
0
    def handle_one_request(self, rline, message):
        self.close()

        # headers
        headers = http.client.HTTPMessage()
        for hdr, val in message.headers:
            headers[hdr] = val

        if 'websocket' in headers.get('UPGRADE', '').lower():
            # init ws
            wsclient = WebSocketProto()
            status, headers = wsclient.serve(
                headers, self.transport, self.rstream)

            write = self.transport.write
            write(b'HTTP/1.1 ' + status.encode())
            for hdr in headers:
                write(hdr)
            write(b'\r\n')

            if status.startswith('101'):
                # start websocket

                @tulip.coroutine
                def rstream():
                    while True:
                        try:
                            data = yield from wsclient.receive()
                            if not data:
                                break
                        except:
                            break

                        data = data.strip()
                        print(data)
                        for wsc in self._connections:
                            if wsc is not wsclient:
                                wsc.send(data.encode())

                print('Someone joined.')
                for wsc in self._connections:
                    wsc.send(b'Someone joined.')

                self._connections.append(wsclient)
                t = tulip.Task(rstream())
                done, pending = yield from tulip.wait([t])
                assert t in done
                assert not pending
                self._connections.remove(wsclient)

                print('Someone disconnected.')
                for wsc in self._connections:
                    wsc.send(b'Someone disconnected.')
        else:
            write = self.transport.write
            write(b'HTTP/1.0 200 Ok\r\n')
            write(b'Content-type: text/html\r\n')
            write(b'\r\n')
            write(WS_SRV_HTML)
Ejemplo n.º 2
0
def main(loop):
    # program which prints evaluation of each expression from stdin
    code = r'''if 1:
                   import os
                   def writeall(fd, buf):
                       while buf:
                           n = os.write(fd, buf)
                           buf = buf[n:]
                   while True:
                       s = os.read(0, 1024)
                       if not s:
                           break
                       s = s.decode('ascii')
                       s = repr(eval(s)) + '\n'
                       s = s.encode('ascii')
                       writeall(1, s)
                   '''

    # commands to send to input
    commands = iter([b"1+1\n",
                     b"2**16\n",
                     b"1/3\n",
                     b"'x'*50",
                     b"1/0\n"])

    # start subprocess and wrap stdin, stdout, stderr
    p = Popen([sys.executable, '-c', code],
              stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stdin = connect_write_pipe(p.stdin)
    stdout = connect_read_pipe(p.stdout)
    stderr = connect_read_pipe(p.stderr)

    # interact with subprocess
    name = {stdout:'OUT', stderr:'ERR'}
    registered = {tulip.Task(stderr.readline()): stderr,
                  tulip.Task(stdout.readline()): stdout}
    while registered:
        # write command
        cmd = next(commands, None)
        if cmd is None:
            stdin.close()
        else:
            print('>>>', cmd.decode('ascii').rstrip())
            stdin.write(cmd)

        # get and print lines from stdout, stderr
        timeout = None
        while registered:
            done, pending = yield from tulip.wait(
                registered, timeout=timeout, return_when=tulip.FIRST_COMPLETED)
            if not done:
                break
            for f in done:
                stream = registered.pop(f)
                res = f.result()
                print(name[stream], res.decode('ascii').rstrip())
                if res != b'':
                    registered[tulip.Task(stream.readline())] = stream
            timeout = 0.0
Ejemplo n.º 3
0
 def handle_noargs(self, **options):
     clients = [run(row, col, SIZE)
                for row in range(SIZE)
                for col in range(SIZE)]
     try:
         tulip.get_event_loop().run_until_complete(tulip.wait(clients))
     except KeyboardInterrupt:
         pass
Ejemplo n.º 4
0
    def test_exception_waiter(self):
        buffer = parsers.DataQueue(loop=self.loop)

        @tulip.coroutine
        def set_err():
            buffer.set_exception(ValueError())

        t1 = tulip.Task(buffer.read(), loop=self.loop)
        t2 = tulip.Task(set_err(), loop=self.loop)

        self.loop.run_until_complete(tulip.wait([t1, t2], loop=self.loop))

        self.assertRaises(ValueError, t1.result)
Ejemplo n.º 5
0
    def close(self, code=1000, reason=""):
        """
        This task performs the closing handshake.

        This is the expected way to terminate a connection on the server side.

        It waits for the other end to complete the handshake. It doesn't do
        anything once the connection is closed.

        The `code` must be an :class:`int` and the `reason` a :class:`str`.
        """
        if self.state == "OPEN":
            # 7.1.2. Start the WebSocket Closing Handshake
            self.close_code, self.close_reason = code, reason
            self.write_frame(OP_CLOSE, serialize_close(code, reason))
            # 7.1.3. The WebSocket Closing Handshake is Started
            self.state = "CLOSING"

        yield from tulip.wait([self.closing_handshake], timeout=self.timeout)
        yield from tulip.wait([self.close_waiter], timeout=self.timeout)

        if self.state != "CLOSED":
            self.transport.close()
Ejemplo n.º 6
0
    def test_handle_cancel(self):
        log = unittest.mock.Mock()
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol(transport, log=log, debug=True)

        srv.handle_request = unittest.mock.Mock()

        @tulip.task
        def cancel():
            srv._request_handler.cancel()

        self.loop.run_until_complete(
            tulip.wait([srv._request_handler, cancel()]))
        self.assertTrue(log.debug.called)
Ejemplo n.º 7
0
    def test_handle_cancel(self):
        log = unittest.mock.Mock()
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol(log=log, debug=True, loop=self.loop)
        srv.connection_made(transport)

        srv.handle_request = unittest.mock.Mock()

        @tulip.coroutine
        def cancel():
            srv._request_handler.cancel()

        self.loop.run_until_complete(
            tulip.wait([srv._request_handler, cancel()], loop=self.loop))
        self.assertTrue(log.debug.called)
Ejemplo n.º 8
0
def chat(name, url, wsclient):
    yield from wsclient.connect(url)
    print('Connected.')

    # stdin reader
    stream = tulip.StreamReader()

    def cb():
        stream.feed_data(sys.stdin.readline().encode())

    event_loop = tulip.get_event_loop()
    event_loop.add_reader(sys.stdin.fileno(), cb)

    yield from tulip.wait(
        [rstream(wsclient), wstream(name, wsclient, stream)],
        return_when=tulip.FIRST_COMPLETED)
Ejemplo n.º 9
0
    def test_handle_cancel(self):
        log = unittest.mock.Mock()
        transport = unittest.mock.Mock()

        srv = server.ServerHttpProtocol(log=log, debug=True)
        srv.connection_made(transport)

        srv.handle_request = unittest.mock.Mock()

        @tulip.task
        def cancel():
            srv._request_handler.cancel()

        self.loop.run_until_complete(
            tulip.wait([srv._request_handler, cancel()]))
        self.assertTrue(log.debug.called)
Ejemplo n.º 10
0
    def handle_noargs(self, **options):
        center = options['center']
        pattern = options['pattern']
        size = options['size']
        speed = options['speed']
        steps = options['steps']
        wrap = options['wrap']

        if pattern is None:
            states = [[None] * size] * size
        else:
            states = self.parse_pattern(pattern, size, center)

        clients = [
            run(row, col, size, wrap, speed, steps, states[row][col])
            for row in range(size) for col in range(size)
        ]

        try:
            tulip.get_event_loop().run_until_complete(reset(size))
            tulip.get_event_loop().run_until_complete(tulip.wait(clients))
        except KeyboardInterrupt:
            pass
Ejemplo n.º 11
0
 def handle_noargs(self, **options):
     self.count = 0
     connections = [self.test_echo() for _ in range(self.CLIENTS)]
     tulip.get_event_loop().run_until_complete(tulip.wait(connections))
     assert self.count == 0
Ejemplo n.º 12
0
def test(pool):
    t1 = tulip.Task(main(pool))
    t2 = tulip.Task(main(pool))
    t3 = tulip.Task(main(pool))
    t4 = tulip.Task(main(pool))
    yield from tulip.wait([t1, t2, t3, t4])
Ejemplo n.º 13
0
def main(loop):
    # program which prints evaluation of each expression from stdin
    code = r'''if 1:
                   import os
                   def writeall(fd, buf):
                       while buf:
                           n = os.write(fd, buf)
                           buf = buf[n:]
                   while True:
                       s = os.read(0, 1024)
                       if not s:
                           break
                       s = s.decode('ascii')
                       s = repr(eval(s)) + '\n'
                       s = s.encode('ascii')
                       writeall(1, s)
                   '''

    # commands to send to input
    commands = iter([b"1+1\n", b"2**16\n", b"1/3\n", b"'x'*50", b"1/0\n"])

    # start subprocess and wrap stdin, stdout, stderr
    p = Popen([sys.executable, '-c', code],
              stdin=PIPE,
              stdout=PIPE,
              stderr=PIPE)

    stdin = yield from connect_write_pipe(p.stdin)
    stdout, stdout_transport = yield from connect_read_pipe(p.stdout)
    stderr, stderr_transport = yield from connect_read_pipe(p.stderr)

    # interact with subprocess
    name = {stdout: 'OUT', stderr: 'ERR'}
    registered = {
        tulip.Task(stderr.readline()): stderr,
        tulip.Task(stdout.readline()): stdout
    }
    while registered:
        # write command
        cmd = next(commands, None)
        if cmd is None:
            stdin.close()
        else:
            print('>>>', cmd.decode('ascii').rstrip())
            stdin.write(cmd)

        # get and print lines from stdout, stderr
        timeout = None
        while registered:
            done, pending = yield from tulip.wait(
                registered, timeout=timeout, return_when=tulip.FIRST_COMPLETED)
            if not done:
                break
            for f in done:
                stream = registered.pop(f)
                res = f.result()
                print(name[stream], res.decode('ascii').rstrip())
                if res != b'':
                    registered[tulip.Task(stream.readline())] = stream
            timeout = 0.0

    stdout_transport.close()
    stderr_transport.close()
Ejemplo n.º 14
0
def start(client, data):
    coroutines = [foo(client), bar(client)]
    done, pending = yield from tulip.wait(coroutines)
    for future in done:
        client.log(future.result())
Ejemplo n.º 15
0
def main():
    clients = [client(num) for num in range(CLIENT_COUNT)]
    yield from tulip.wait(clients)
Ejemplo n.º 16
0
def start(client, data):
    coroutines = [foo(client), bar(client)]
    done, pending = yield from tulip.wait(coroutines)
    for future in done:
        client.log(future.result())