Beispiel #1
0
    def _run(self):
        def factory():
            return tulip.http.WSGIServerHttpProtocol(self.wsgi,
                                                     readpayload=True)

        # insert sockets to event_loop
        for sock in self.sockets:
            self.ev_loop.start_serving(factory, sock=sock)

        # If our parent changed then we shut down.
        pid = os.getpid()
        try:
            while self.alive:
                self.notify()

                if pid == os.getpid() and self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s", self)
                    break

                yield from tulip.sleep(1.0)
        except KeyboardInterrupt:
            pass

        tulip.get_event_loop().stop()
        tulip.get_event_loop().close()
Beispiel #2
0
    def _run(self):
        def factory():
            return tulip.http.WSGIServerHttpProtocol(
                self.wsgi, readpayload=True)

        # insert sockets to event_loop
        for sock in self.sockets:
            self.ev_loop.start_serving(factory, sock=sock)

        # If our parent changed then we shut down.
        pid = os.getpid()
        try:
            while self.alive:
                self.notify()

                if pid == os.getpid() and self.ppid != os.getppid():
                    self.log.info("Parent changed, shutting down: %s", self)
                    break

                yield from tulip.sleep(1.0)
        except KeyboardInterrupt:
            pass

        tulip.get_event_loop().stop()
        tulip.get_event_loop().close()
Beispiel #3
0
    def init_process(self):
        # create new event_loop after fork
        tulip.get_event_loop().close()

        self.ev_loop = tulip.new_event_loop()
        tulip.set_event_loop(self.ev_loop)

        super().init_process()
 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
Beispiel #5
0
    def init_process(self):
        # create new event_loop after fork
        tulip.get_event_loop().close()

        self.ev_loop = tulip.new_event_loop()
        tulip.set_event_loop(self.ev_loop)

        super().init_process()
    def test_gameoflife(self):
        # Reduce size before opening the browser so it gets the right size.
        tulip.get_event_loop().run_until_complete(client.reset(5))

        # This is just for the eye candy.
        self.selenium.get(self.live_server_url + reverse('gameoflife.views.watch'))

        # Run the game, with and without a pattern.
        call_command('gameoflife', size=5, speed=100, steps=5)

        call_command('gameoflife', size=5, speed=100, steps=5,
                                   pattern='gameoflife/patterns/blinker')
Beispiel #7
0
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)
        loop.add_signal_handler(signal.SIGINT, stop)

        # heartbeat
        tulip.Task(self.heartbeat())

        tulip.get_event_loop().run_forever()
        os._exit(0)
Beispiel #8
0
def connect_read_pipe(file):
    loop = tulip.get_event_loop()
    stream_reader = streams.StreamReader(loop=loop)
    def factory():
        return streams.StreamReaderProtocol(stream_reader)
    transport, _ = yield from loop.connect_read_pipe(factory, file)
    return stream_reader, transport
Beispiel #9
0
    def connection_made(self, transport):
        print('connection made')
        self.transport = transport

        # start 5 seconds timeout timer
        self.h_timeout = tulip.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)
Beispiel #10
0
def bar(client):
    client.log('Starting bar work in another thread.')
    loop = tulip.get_event_loop()
    widgets = yield from loop.run_in_executor(None, bar_synchronous)
    for widget in widgets:
        client.append_widget('bar', widget)
    return 'bar is done!'
    def __init__(self):
        self.loop = tulip.get_event_loop()
        args = ARGS.parse_args()
        if ':' in args.host:
            args.host, port = args.host.split(':', 1)
            args.port = int(port)

        if args.iocp:
            from tulip import windows_events
            sys.argv.remove('--iocp')
            logging.info('using iocp')
            el = windows_events.ProactorEventLoop()
            tulip.set_event_loop(el)

        if args.ssl:
            here = os.path.join(os.path.dirname(__file__), 'tests')

            if args.certfile:
                certfile = args.certfile or os.path.join(here, 'sample.crt')
                keyfile = args.keyfile or os.path.join(here, 'sample.key')
            else:
                certfile = os.path.join(here, 'sample.crt')
                keyfile = os.path.join(here, 'sample.key')

            sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            sslcontext.load_cert_chain(certfile, keyfile)
        else:
            sslcontext = None
        self.ssl = sslcontext

        self.args = args
        self.workers = []
Beispiel #12
0
    def _negotiate(self, call_after=None):
        """
        Negotiate options before prompting for input, this method calls itself
        every CONNECT_DEFERED up to the greater of the value CONNECT_MAXWAIT.

        Negotiation completes when all ``pending_options`` of the
        TelnetStreamReader have completed. Any options not negotiated
        are displayed to the client as a warning, and ``display_prompt()``
        is called for the first time, unless ``call_after`` specifies another
        callback.
        """
        if call_after is None:
            call_after = self.first_prompt
        assert callable(call_after), call_after
        if self._closing:
            return
        loop = tulip.get_event_loop()
        pending = [
            telopt._name_commands(opt)
            for (opt, val) in self.stream.pending_option.items() if val
        ]
        if self.duration < self.CONNECT_MINWAIT or (
                pending and self.duration < self.CONNECT_MAXWAIT):
            loop.call_later(self.CONNECT_DEFERED, self._negotiate, call_after)
            return
        elif pending:
            self.log.warn('negotiate failed for {}.'.format(pending))
            self.echo('\r\nnegotiate failed for {}.'.format(pending))
        loop.call_soon(call_after)
Beispiel #13
0
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)

        loop.add_signal_handler(signal.SIGINT, stop)

        # heartbeat
        self.heartbeat()

        tulip.get_event_loop().run_forever()
        os._exit(0)
Beispiel #14
0
    def __init__(self):
        self.loop = tulip.get_event_loop()
        args = ARGS.parse_args()
        if ':' in args.host:
            args.host, port = args.host.split(':', 1)
            args.port = int(port)

        if args.iocp:
            from tulip import windows_events
            sys.argv.remove('--iocp')
            logging.info('using iocp')
            el = windows_events.ProactorEventLoop()
            tulip.set_event_loop(el)

        if args.ssl:
            here = os.path.join(os.path.dirname(__file__), 'tests')

            if args.certfile:
                certfile = args.certfile or os.path.join(here, 'sample.crt')
                keyfile = args.keyfile or os.path.join(here, 'sample.key')
            else:
                certfile = os.path.join(here, 'sample.crt')
                keyfile = os.path.join(here, 'sample.key')

            sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            sslcontext.load_cert_chain(certfile, keyfile)
        else:
            sslcontext = None
        self.ssl = sslcontext

        self.args = args
        self.workers = []
Beispiel #15
0
def scanport(port):
    ippat = "10.1.10.%d"
    loop = tulip.get_event_loop()
    loop.add_signal_handler(signal.SIGINT, loop.stop)
    futs = []
    for x in range(1, 255):
        host = ippat % x
        print("trying", host, port, end="\r", flush=True)
        fut = tulip.Task(loop.create_connection(Scanner, host, port), timeout=1)
        futs.append(fut)
        loop.run_until_complete(tulip.sleep(0.001))
    print()
    for fut in futs:
        try:
            loop.run_until_complete(fut)
        except tulip.CancelledError:
            pass
        except os.error as exc:
            if exc.errno == 24:
                print()
                print(exc)
        except Exception as exc:
            print()
            print(exc)
    print()
    loop.call_later(1, loop.stop)
    loop.run_forever()
def download(url, lock):
    loop = tulip.get_event_loop()
    print('DOWNLOADING', url)
    retriable_download(url,
                       on_ok=lambda x: print("RECEIVED OK", len(x)),
                       on_error=lambda exc: print("RECEIVED ERROR", exc),
                       loop=loop)
Beispiel #17
0
    def _negotiate(self, call_after=None):
        """
        Negotiate options before prompting for input, this method calls itself
        every CONNECT_DEFERED up to the greater of the value CONNECT_MAXWAIT.

        Negotiation completes when all ``pending_options`` of the
        TelnetStreamReader have completed. Any options not negotiated
        are displayed to the client as a warning, and ``display_prompt()``
        is called for the first time, unless ``call_after`` specifies another
        callback.
        """
        if call_after is None:
            call_after = self.first_prompt
        assert callable(call_after), call_after
        if self._closing:
            return
        loop = tulip.get_event_loop()
        pending = [telopt._name_commands(opt)
                for (opt, val) in self.stream.pending_option.items()
                if val]
        if self.duration < self.CONNECT_MINWAIT or (
                pending and self.duration < self.CONNECT_MAXWAIT):
            loop.call_later(self.CONNECT_DEFERED, self._negotiate, call_after)
            return
        elif pending:
            self.log.warn('negotiate failed for {}.'.format(pending))
            self.echo('\r\nnegotiate failed for {}.'.format(pending))
        loop.call_soon(call_after)
Beispiel #18
0
def main():
    loop = tulip.get_event_loop()
    loop.add_signal_handler(signal.SIGINT, loop.stop)
    f = loop.start_serving(HttpServer, '127.0.0.1', 8080)
    x = loop.run_until_complete(f)
    print('serving on', x.getsockname())
    loop.run_forever()
def retriable_download(url, on_ok, on_error, retries=2, sleep=1, loop=None):
    loop = loop or tulip.get_event_loop()
    parsed = urlparse(url)
    host = parsed.hostname
    port = parsed.port or 80
    path = parsed.path

    def retry_on_error(exc):
        print("ERROR {}. Retrying after {} s. Number or retries {}"
                .format(exc, sleep, retries))
        loop.call_later(
            sleep,
            retriable_download,
            url, on_ok, on_error, retries-1, sleep, loop)

    error_cb = on_error if retries == 0 else retry_on_error

    # We wait for addrinfo to be resolved. When it is this func will be called.
    def got_connection(transport, protocol):
        # Set callbacks on data received
        protocol.on_ok = on_ok
        protocol.on_error = error_cb
        # Send request to transport. Responce will be handled by callbacks.
        request = (
            b'GET ' + path.encode('ascii') + b' HTTP/1.1\n' +
            b'HOST: ' + host.encode('ascii') + b'\n' +
            b'\r\n')
        transport.write(request)
    # We do not want our callback's based code to be mixed in corutines code.
    create_connection_with_cb(
        got_connection, error_cb, loop, HTTPProtocol, host, port)
def retriable_download(url, on_ok, on_error, retries=2, sleep=1, loop=None):
    loop = loop or tulip.get_event_loop()
    parsed = urlparse(url)
    host = parsed.hostname
    port = parsed.port or 80
    path = parsed.path

    def retry_on_error(exc):
        print("ERROR {}. Retrying after {} s. Number or retries {}".format(
            exc, sleep, retries))
        loop.call_later(sleep, retriable_download, url, on_ok, on_error,
                        retries - 1, sleep, loop)

    error_cb = on_error if retries == 0 else retry_on_error

    # We wait for addrinfo to be resolved. When it is this func will be called.
    def got_connection(transport, protocol):
        # Set callbacks on data received
        protocol.on_ok = on_ok
        protocol.on_error = error_cb
        # Send request to transport. Responce will be handled by callbacks.
        request = (b'GET ' + path.encode('ascii') + b' HTTP/1.1\n' +
                   b'HOST: ' + host.encode('ascii') + b'\n' + b'\r\n')
        transport.write(request)

    # We do not want our callback's based code to be mixed in corutines code.
    create_connection_with_cb(got_connection, error_cb, loop, HTTPProtocol,
                              host, port)
Beispiel #21
0
    def connection_made(self, transport):
        print('connection made')
        self.transport = transport

        # start 5 seconds timeout timer
        self.h_timeout = tulip.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)
    def test_gameoflife(self):
        # Reduce size before opening the browser so it gets the right size.
        tulip.get_event_loop().run_until_complete(client.reset(5))

        # This is just for the eye candy.
        self.selenium.get(self.live_server_url +
                          reverse('gameoflife.views.watch'))

        # Run the game, with and without a pattern.
        call_command('gameoflife', size=5, speed=100, steps=5)

        call_command('gameoflife',
                     size=5,
                     speed=100,
                     steps=5,
                     pattern='gameoflife/patterns/blinker')
Beispiel #23
0
def scanport(port):
    ippat = '10.1.10.%d'
    loop = tulip.get_event_loop()
    loop.add_signal_handler(signal.SIGINT, loop.stop)
    futs = []
    for x in range(1, 255):
        host = ippat % x
        print('trying', host, port, end='\r', flush=True)
        fut = tulip.Task(loop.create_connection(Scanner, host, port),
                          timeout=1)
        futs.append(fut)
        loop.run_until_complete(tulip.sleep(0.001))
    print()
    for fut in futs:
        try:
            loop.run_until_complete(fut)
        except tulip.CancelledError:
            pass
        except os.error as exc:
            if exc.errno == 24:
                print()
                print(exc)
        except Exception as exc:
            print()
            print(exc)
    print()
    loop.call_later(1, loop.stop)
    loop.run_forever()
def main():
    url = len(sys.argv) > 1 and sys.argv[1] or 'http://example.com/'
    loop = tulip.get_event_loop()
    lock = tulip.locks.Lock()
    loop.call_soon(download, 'http://example.com', lock)
    loop.call_later(4, download, 'http://google.com', lock)
    loop.call_later(8, download, 'http://some_lame_address_1111.com', lock)
    loop.run_until_complete(tulip.sleep(20))
Beispiel #25
0
    def data_received(self, data):
        print('data received: ', data.decode())
        self.transport.write(b'Re: ' + data)

        # restart timeout timer
        self.h_timeout.cancel()
        self.h_timeout = tulip.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)
Beispiel #26
0
    def data_received(self, data):
        print('data received: ', data.decode())
        self.transport.write(b'Re: ' + data)

        # restart timeout timer
        self.h_timeout.cancel()
        self.h_timeout = tulip.get_event_loop().call_later(
            self.TIMEOUT, self.timeout)
def main():
    url = len(sys.argv) > 1 and sys.argv[1] or 'http://example.com/'
    loop = tulip.get_event_loop()
    lock = tulip.locks.Lock()
    loop.call_soon(download, 'http://example.com', lock)
    loop.call_later(4, download, 'http://google.com', lock)
    loop.call_later(8, download, 'http://some_lame_address_1111.com', lock)
    loop.run_until_complete(tulip.sleep(20))
Beispiel #28
0
def connect_read_pipe(file):
    loop = tulip.get_event_loop()
    stream_reader = streams.StreamReader(loop=loop)

    def factory():
        return streams.StreamReaderProtocol(stream_reader)

    transport, _ = yield from loop.connect_read_pipe(factory, file)
    return stream_reader, transport
Beispiel #29
0
def connect(dsn=None, *, loop=None):
    if loop is None:
        loop = tulip.get_event_loop()

    conn = psycopg2.connect(
        dsn=dsn,
        cursor_factory=functools.partial(Cursor, loop=loop), async=1)
    yield from wait(conn, loop)
    return conn
def download(url, lock):
    loop = tulip.get_event_loop()
    print('DOWNLOADING', url)
    retriable_download(
        url,
        on_ok=lambda x: print("RECEIVED OK", len(x)),
        on_error=lambda exc: print("RECEIVED ERROR", exc),
        loop=loop
    )
Beispiel #31
0
 def test_drive_client():
     loop = tulip.get_event_loop()
     print("in test driver")
     client = AsyncClient(loop=loop)
     yield from client.connect()
     resp = yield from client.set(b'foozle', b'yay')
     print(resp)
     val = yield from client.get(b'foozle')
     print(val)
     return val
Beispiel #32
0
    def first_prompt(self, call_after=None):
        """ XXX First time prompt fire
        """
        call_after = self.display_prompt if call_after is None else call_after
        assert callable(call_after), call_after

        self.log.info(self.about_connection())
        # conceivably, you could use various callback mechanisms to
        # relate to authenticating or other multi-state login process.
        loop = tulip.get_event_loop()
        loop.call_soon(call_after)
Beispiel #33
0
    def __init__(self, *, log=logging, debug=False,
                 keep_alive=None, loop=None, **kwargs):
        self.__dict__.update(kwargs)
        self.log = log
        self.debug = debug

        self._keep_alive_period = keep_alive  # number of seconds to keep alive

        if keep_alive and loop is None:
            loop = tulip.get_event_loop()
        self._loop = loop
Beispiel #34
0
    def first_prompt(self, call_after=None):
        """ XXX First time prompt fire
        """
        call_after = self.display_prompt if call_after is None else call_after
        assert callable(call_after), call_after

        self.log.info(self.about_connection())
        # conceivably, you could use various callback mechanisms to
        # relate to authenticating or other multi-state login process.
        loop = tulip.get_event_loop()
        loop.call_soon(call_after)
Beispiel #35
0
    def __init__(self, *, log=logging, debug=False,
                 keep_alive=None, loop=None, **kwargs):
        self.__dict__.update(kwargs)
        self.log = log
        self.debug = debug

        self._keep_alive_period = keep_alive  # number of seconds to keep alive

        if keep_alive and loop is None:
            loop = tulip.get_event_loop()
        self._loop = loop
Beispiel #36
0
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)
        loop.add_signal_handler(signal.SIGINT, stop)

        f = loop.start_serving(
            lambda: HttpServer(debug=True, keep_alive=75), sock=self.sock)
        x = loop.run_until_complete(f)[0]
        print('Starting srv worker process {} on {}'.format(
            os.getpid(), x.getsockname()))

        # heartbeat
        self.heartbeat()

        tulip.get_event_loop().run_forever()
        os._exit(0)
Beispiel #37
0
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)
        loop.add_signal_handler(signal.SIGINT, stop)

        f = loop.start_serving(
            self.protocol_factory, sock=self.sock, ssl=self.ssl)
        x = loop.run_until_complete(f)[0]
        print('Starting srv worker process {} on {}'.format(
            os.getpid(), x.getsockname()))

        # heartbeat
        self.heartbeat()

        tulip.get_event_loop().run_forever()
        os._exit(0)
Beispiel #38
0
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)

        loop.add_signal_handler(signal.SIGINT, stop)

        f = loop.start_serving(lambda: HttpServer(debug=True, keep_alive=75),
                               sock=self.sock)
        x = loop.run_until_complete(f)[0]
        print('Starting srv worker process {} on {}'.format(
            os.getpid(), x.getsockname()))

        # heartbeat
        self.heartbeat()

        tulip.get_event_loop().run_forever()
        os._exit(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
    def start(self):
        # start server
        self.loop = loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)

        def stop():
            self.loop.stop()
            os._exit(0)

        loop.add_signal_handler(signal.SIGINT, stop)

        f = loop.start_serving(self.protocol_factory,
                               sock=self.sock,
                               ssl=self.ssl)
        x = loop.run_until_complete(f)[0]
        print('Starting srv worker process {} on {}'.format(
            os.getpid(), x.getsockname()))

        # heartbeat
        self.heartbeat()

        tulip.get_event_loop().run_forever()
        os._exit(0)
Beispiel #41
0
    def __init__(self, transport, *, log=logging, debug=False,
                 keep_alive=None, loop=None, **kwargs):
        self.__dict__.update(kwargs)
        self.log = log
        self.debug = debug

        self._keep_alive_period = keep_alive  # number of seconds to keep alive

        if keep_alive and loop is None:
            loop = tulip.get_event_loop()
        self._loop = loop
        self.transport = transport
        self.transport.register_protocol(self)
        self.stream = tulip.StreamBuffer()
        self._request_handler = self.start()
Beispiel #42
0
def main():
    loop = tulip.get_event_loop()

    c = Crawler(sys.argv[1], loop)
    c.run()

    try:
        loop.add_signal_handler(signal.SIGINT, loop.stop)
    except RuntimeError:
        pass
    loop.run_forever()
    print('todo:', len(c.todo))
    print('busy:', len(c.busy))
    print('done:', len(c.done), '; ok:', sum(c.done.values()))
    print('tasks:', len(c.tasks))
Beispiel #43
0
def main():
    name = input('Please enter your name: ').encode()

    url = 'http://localhost:8080'
    wsclient = WebSocketProto()

    loop = tulip.get_event_loop()
    try:
        loop.add_signal_handler(signal.SIGINT, loop.stop)
    except RuntimeError:
        pass
    try:
        loop.run_until_complete(chat(name, url, wsclient))
    except:
        pass
Beispiel #44
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)
Beispiel #45
0
def run(addr, port, wsgi_handler, **options):
    """
    Alternate version of django.core.servers.basehttp.run running on Tulip.
    """
    loop = tulip.get_event_loop()
    if loop is None:
        # The auto-reloader runs in the main thread and starts the server
        # in another thread. Create an event loop for that thread.
        loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)
    # The code that reads environ['wsgi.input'] is deep inside Django and hard
    # to make asynchronous. Pre-loading the payload is the simplest option.
    protocol_factory = lambda: tulip.http.WSGIServerHttpProtocol(
            wsgi_handler, readpayload=True)
    loop.start_serving(protocol_factory, addr, port)
    loop.run_forever()
Beispiel #46
0
def run(addr, port, wsgi_handler, ipv6=False, threading=False):
    """
    Alternate version of django.core.servers.basehttp.run running on Tulip.
    """
    assert not ipv6, "IPv6 isn't supported"
    assert threading, "threading is irrelevant"
    httpd = WSGIServer(addr, port, WSGIRequestHandler)
    httpd.set_app(wsgi_handler)
    # The auto-reloader runs in the main thread and starts the server in
    # another thread. Since the event loop in thread-local we must create one.
    loop = tulip.get_event_loop()
    if loop is None:
        loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)
    loop.start_serving(httpd, addr, port)
    loop.run_forever()
Beispiel #47
0
def run(addr, port, wsgi_handler, **options):
    """
    Alternate version of django.core.servers.basehttp.run running on Tulip.
    """
    loop = tulip.get_event_loop()
    if loop is None:
        # The auto-reloader runs in the main thread and starts the server
        # in another thread. Create an event loop for that thread.
        loop = tulip.new_event_loop()
        tulip.set_event_loop(loop)
    # The code that reads environ['wsgi.input'] is deep inside Django and hard
    # to make asynchronous. Pre-loading the payload is the simplest option.
    protocol_factory = lambda: tulip.http.WSGIServerHttpProtocol(
        wsgi_handler, readpayload=True)
    loop.start_serving(protocol_factory, addr, port)
    loop.run_forever()
def download(url, retries=3, sleep=5, loop=None):
    loop = loop or tulip.get_event_loop()
    print('DOWNLOADING', url)
    data = None
    for i in range(retries):
        try:
            data = yield from download_single(loop, url)
        except Exception:
            print("FAILED TO DOWNLOAD", url, "RETRYING.")
            yield from tulip.sleep(sleep)
        else:
            break
    else:
        print("FAILED TO DOWNLOAD", url)
        return
    return data
Beispiel #49
0
def client(num):
    loop = tulip.get_event_loop()
    print("Connect client #{0} to {1[0]}:{1[1]}".format(num, ADDRESS))
    transp, stream = yield from loop.create_connection(
                                tulip.StreamProtocol, *ADDRESS)
    reader = stream.set_parser(tulip.lines_parser())
    try:
        msg = MESSAGE
        assert '\n' not in msg
        msg += ' #{}'.format(num)
        print("Send message '{}'".format(msg))
        msg += '\n'
        transp.write(msg.encode('utf-8'))
        response = (yield from reader.read())[:-1].decode('utf-8')
        print("Response '{}'".format(response, num))
    finally:
        print('Close connection of client #{}'.format(num))
        transp.close()
Beispiel #50
0
 def run(self, handler):
     def wsgi_app(env, start):
         def start_response(status_line, headerlist, exc_info=None):
             status_code = status_line.split(' ', 1)[0]
             headerdict = dict(map(lambda x: (x[0].lower(), x[1]), headerlist))
             length = headerdict.get('content-length', 0)
             logger.info('{} {} {} {}'.format(env['REQUEST_METHOD'],
                 env['RAW_URI'], status_code, length))
             return start(status_line, headerlist, exc_info)
         return handler(env, start_response)
     loop = tulip.get_event_loop()
     f = loop.create_server(
             lambda: WSGIServerHttpProtocol(wsgi_app, loop = loop, readpayload=True),
             self.host, self.port)
     loop.run_until_complete(f)
     try:
         loop.run_forever()
     except KeyboardInterrupt:
         pass
Beispiel #51
0
    def run(self, host='127.0.0.1', port=8000, call_soon=None):
        if sys.platform == 'win32':
            from tulip.windows_events import ProactorEventLoop
            loop = ProactorEventLoop()
            tulip.set_event_loop(loop)
        else:
            loop = tulip.get_event_loop()

        self.loop = loop

        task = loop.start_serving(lambda: HttpServer(debug=True, app=self),
                                  host, port)
        socks = loop.run_until_complete(task)

        if call_soon:
            loop.call_soon(call_soon)

        print('Serving on', socks[0].getsockname())
        try:
            loop.run_forever()
        except KeyboardInterrupt:
            loop.close()
Beispiel #52
0
def main():
    args = ARGS.parse_args()

    if ':' in args.host:
        args.host, port = args.host.split(':', 1)
        args.port = int(port)

    if args.iocp:
        from tulip import windows_events
        sys.argv.remove('--iocp')
        logging.info('using iocp')
        el = windows_events.ProactorEventLoop()
        tulip.set_event_loop(el)

    if args.ssl:
        here = os.path.join(os.path.dirname(__file__), 'tests')

        if args.certfile:
            certfile = args.certfile or os.path.join(here, 'sample.crt')
            keyfile = args.keyfile or os.path.join(here, 'sample.key')
        else:
            certfile = os.path.join(here, 'sample.crt')
            keyfile = os.path.join(here, 'sample.key')

        sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        sslcontext.load_cert_chain(certfile, keyfile)
    else:
        sslcontext = None

    loop = tulip.get_event_loop()
    f = loop.start_serving(
        lambda: HttpServer(debug=True, keep_alive=75), args.host, args.port,
        ssl=sslcontext)
    socks = loop.run_until_complete(f)
    print('serving on', socks[0].getsockname())
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
Beispiel #53
0
def main():
    import logging
    import locale
    args = ARGS.parse_args()
    if ':' in args.host:
        args.host, port = args.host.split(':', 1)
        args.port = int(port)
    locale.setlocale(locale.LC_ALL, '')
    enc = locale.getpreferredencoding()
    log = logging.getLogger()
    log_const = args.loglevel.upper()
    assert (log_const in dir(logging)
            and isinstance(getattr(logging, log_const), int)), args.loglevel
    log.setLevel(getattr(logging, log_const))
    log.debug('default_encoding is {}'.format(enc))

    loop = tulip.get_event_loop()
    func = loop.start_serving(lambda: TelnetServer(default_encoding=enc),
                              args.host, args.port)

    for sock in loop.run_until_complete(func):
        logging.info('Listening on %s', sock.getsockname())
    loop.run_forever()
Beispiel #54
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
Beispiel #55
0
import tulip
from tulip import http

@tulip.coroutine
def download(url):
    response = yield from http.request('GET', url)
    for k, v in response.items():
        print('{}: {}'.format(k, v[:80]))

    data = yield from response.read()
    print('\nReceived {} bytes.\n'.format(len(data)))

if __name__ == '__main__':
    loop = tulip.get_event_loop()
    coroutine = download('http://omegafeihong.tumblr.com')
    loop.run_until_complete(coroutine)
Beispiel #56
0
def connect_write_pipe(file):
    loop = tulip.get_event_loop()
    protocol = protocols.Protocol()
    transport, _ = yield from loop.connect_write_pipe(tulip.Protocol, file)
    return transport