示例#1
0
文件: web.py 项目: wsb310/hagworm
    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()
示例#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
    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
示例#4
0
文件: event.py 项目: Zidoing/hagworm
    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))
    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
示例#6
0
文件: web.py 项目: wsb310/hagworm
    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
            )
        )
示例#7
0
文件: event.py 项目: Tylerlog/hagworm
    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))
示例#8
0
文件: event.py 项目: Zidoing/hagworm
    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))
    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
示例#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
示例#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
示例#12
0
文件: event.py 项目: Zidoing/hagworm
    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)
示例#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
示例#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()
示例#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()
示例#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
示例#17
0
    def _refresh_online(self):

        self._global_dict[r'health_refresh_time'] = Utils.loop_time()
示例#18
0
 async def _do_acton():
     return Utils.randint(0, 0xffff)
示例#19
0
文件: home.py 项目: wsb310/hagworm
    def delete(self):

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

        super().open()

        self._msg_listen_task = Utils.create_task(self._message_listener())
示例#22
0
    def run(self, pids):

        global HIGH_WATER_MARK

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