Ejemplo n.º 1
0
    async def transmit(self, url, file_name=None, *, params=None, cookies=None, headers=None):

        try:

            _range = self.request.headers.get(r'Range', None)

            if _range is not None:

                if headers is None:
                    headers = {r'Range': _range}
                else:
                    headers[r'Range'] = _range

            if not file_name:
                file_name = self._get_file_name(url)

            if file_name:
                self.set_header(
                    r'Content-Disposition',
                    f'attachment;filename={file_name}'
                )

            Utils.create_task(self.fetch(url, params=params, cookies=cookies, headers=headers))

            await self._flush_data()

        except Exception as err:

            self.log.error(err)

        finally:

            self.finish()
Ejemplo n.º 2
0
    async def test_stack_cache(self):

        cache = StackCache()

        ckey = Utils.uuid1()
        cval = Utils.uuid1()

        res1 = cache.has(ckey)
        res2 = cache.size()

        assert not res1 and res2 == 0

        cache.set(ckey, cval, 0.5)

        res3 = cache.has(ckey)
        res4 = cache.get(ckey)
        res5 = cache.size()

        assert res3 and res4 == cval and res5 == 1

        await Utils.sleep(1)

        res6 = cache.has(ckey)
        res7 = cache.get(ckey)
        res8 = cache.size()

        assert not res6 and res7 is None and res8 == 0
Ejemplo n.º 3
0
    async def test_future_with_timeout(self):

        time1 = Utils.loop_time()

        await FutureWithTimeout(1)

        time2 = Utils.loop_time()

        assert Utils.math.floor(time2 - time1) == 1
Ejemplo n.º 4
0
    def __init__(self, redis_pool, channel_name, channel_count):

        super().__init__()

        self._redis_pool = redis_pool

        self._channels = [f'event_bus_{Utils.md5_u32(channel_name)}_{channel}' for channel in range(channel_count)]

        for channel in self._channels:
            Utils.create_task(self._event_listener(channel))
Ejemplo n.º 5
0
    async def test_async_circulator(self):

        time1 = Utils.loop_time()

        async for _ in AsyncCirculator(1, 0xff):
            pass

        time2 = Utils.loop_time()

        assert Utils.math.floor(time2 - time1) == 1
Ejemplo n.º 6
0
    def log_request(self):

        Utils.log.info(
            '\n---------- request arguments ----------\n' +
            Utils.json_encode(
                {
                    key: [Utils.basestring(val) for val in items]
                    for key, items in self.request.arguments.items()
                },
                escape_forward_slashes=False, ensure_ascii=False, indent=4
            )
        )
Ejemplo n.º 7
0
    def __init__(self, redis_pool, channel_name, channel_count):

        super().__init__()

        self._redis_pool = redis_pool

        self._channels = [
            r'event_bus_{0}_{1}'.format(Utils.md5_u32(channel_name), channel)
            for channel in range(channel_count)
        ]

        for channel in self._channels:
            Utils.ensure_future(self._event_listener(channel))
Ejemplo n.º 8
0
    async def dispatch(self, _type, *args, **kwargs):

        channel = self._channels[Utils.md5_u32(_type) % len(self._channels)]

        message = {
            r'type': _type,
            r'args': args,
            r'kwargs': kwargs,
        }

        Utils.log.debug(f'event dispatch => channel({channel}) message({message})')

        async with self._redis_pool.get_client() as cache:
            await cache.publish(channel, Utils.pickle_dumps(message))
Ejemplo n.º 9
0
    async def test_slice_tasks(self):
        async def _do_acton():
            await Utils.sleep(1)

        time1 = Utils.loop_time()

        tasks = SliceTasks(3)

        for _ in range(10):
            tasks.append(_do_acton())

        await tasks

        time2 = Utils.loop_time()

        assert Utils.math.floor(time2 - time1) == 4
Ejemplo n.º 10
0
    def online(self):

        health_refresh_time = self._global_dict.get(r'health_refresh_time', 0)

        if health_refresh_time > 0:
            return Utils.loop_time() - health_refresh_time < 60
        else:
            return False
Ejemplo n.º 11
0
    def __init__(self, name, socket_type, address, bind_mode):

        self._name = name if name else Utils.uuid1()[:8]

        self._context = Context.instance()
        self._socket = self._context.socket(socket_type)

        self._address = address
        self._bind_mode = bind_mode
Ejemplo n.º 12
0
    async def _event_assigner(self, channel, message):

        message = Utils.pickle_loads(message)

        Utils.log.debug(f'event handling => channel({channel.name.decode()}) message({message})')

        _type = message.get(r'type', r'')
        args = message.get(r'args', [])
        kwargs = message.get(r'kwargs', {})

        if _type in self._observers:
            self._observers[_type](*args, **kwargs)
Ejemplo n.º 13
0
    async def test_queue_tasks(self):
        async def _do_acton(val):
            await Utils.sleep(val)

        time1 = Utils.loop_time()

        tasks = QueueTasks(5)

        tasks.append(_do_acton(8))

        for _ in range(2):

            tasks.append(_do_acton(4))

            for _ in range(2):
                tasks.append(_do_acton(1))
                tasks.append(_do_acton(1))
                tasks.append(_do_acton(2))

        await tasks

        time2 = Utils.loop_time()

        assert Utils.math.floor(time2 - time1) == 8
Ejemplo n.º 14
0
        def _wrapper(handler, *args, **kwargs):

            auth_header = handler.get_header(r'Authorization')

            try:

                if auth_header:

                    auth_info = Utils.b64_decode(auth_header.split(r' ', 2)[1])

                    if auth_info == f'{self._username}:{self._password}':
                        return func(handler, *args, **kwargs)

            except Exception as err:

                Utils.log.error(err)

            handler.set_header(r'WWW-Authenticate',
                               f'Basic realm="{self._realm}"')
            handler.set_status(401)

            return handler.finish()
Ejemplo n.º 15
0
        def _wrapper(request, *args, **kwargs):

            auth_header = request.get_header(r'Authorization')

            try:

                if auth_header:

                    auth_info = Utils.b64_decode(auth_header.split(r' ', 2)[1])

                    if auth_info == r'{0}:{1}'.format(self._username,
                                                      self._password):
                        return func(request, *args, **kwargs)

            except Exception as err:

                Utils.log.error(err)

            request.set_header(r'WWW-Authenticate',
                               r'Basic realm="{0}"'.format(self._realm))
            request.set_status(401)

            return request.finish()
Ejemplo n.º 16
0
    async def test_key_lower_dict(self):

        temp1 = {
            r'MySQL1': {
                r'MySQL1': Utils.randint(0x10, 0xff),
                r'MYSQL2': Utils.randint(0x10, 0xff),
                r'Mysql3': Utils.randint(0x10, 0xff),
            },
            r'MYSQL2': {
                r'MySQL1': {
                    r'MySQL1': Utils.randint(0x10, 0xff),
                    r'MYSQL2': Utils.randint(0x10, 0xff),
                    r'Mysql3': Utils.randint(0x10, 0xff),
                },
                r'MYSQL2': {
                    r'MySQL1': Utils.randint(0x10, 0xff),
                    r'MYSQL2': Utils.randint(0x10, 0xff),
                    r'Mysql3': Utils.randint(0x10, 0xff),
                },
                r'Mysql3': {
                    r'MySQL1': Utils.randint(0x10, 0xff),
                    r'MYSQL2': Utils.randint(0x10, 0xff),
                    r'Mysql3': Utils.randint(0x10, 0xff),
                },
            },
            r'Mysql3': {
                r'MySQL1': {
                    r'MySQL1': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'MYSQL2': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'Mysql3': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                },
                r'MYSQL2': {
                    r'MySQL1': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'MYSQL2': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'Mysql3': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                },
                r'Mysql3': {
                    r'MySQL1': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'MYSQL2': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                    r'Mysql3': {
                        r'MySQL1': Utils.randint(0x10, 0xff),
                        r'MYSQL2': Utils.randint(0x10, 0xff),
                        r'Mysql3': Utils.randint(0x10, 0xff),
                    },
                },
            },
        }

        temp2 = KeyLowerDict(temp1)

        assert r'my_sql1' in temp2 and r'mysql2' in temp2 and r'mysql3' in temp2
Ejemplo n.º 17
0
    def _refresh_online(self):

        self._global_dict[r'health_refresh_time'] = Utils.loop_time()
Ejemplo n.º 18
0
 async def _do_acton():
     return Utils.randint(0, 0xffff)
Ejemplo n.º 19
0
    def delete(self):

        Utils.call_soon(DataSource().reset_mysql_pool)
        Utils.call_soon(DataSource().reset_mongo_pool)
Ejemplo n.º 20
0
 async def _do_acton():
     await Utils.sleep(1)
     return Utils.randint(0, 0xffff)
Ejemplo n.º 21
0
    def open(self):

        super().open()

        self._msg_listen_task = Utils.create_task(self._message_listener())
Ejemplo n.º 22
0
    def run(self, pids):

        global HIGH_WATER_MARK

        Utils.run_until_complete(self._do_polling(pids, HIGH_WATER_MARK))