def api(self, request): """ 转发客户端和游戏服务器之间的API通信。 :param request: aiohttp.web.Request :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest """ action = request.match_info['action'] session = yield from get_session(request) world_ip = session['world_ip'] if world_ip: if action == 'api_start2' and self.api_start2 is not None: return aiohttp.web.Response(body=self.api_start2, headers=aiohttp.MultiDict({'Content-Type': 'text/plain'})) else: referrer = request.headers.get('REFERER') referrer = referrer.replace(request.host, world_ip) referrer = referrer.replace('https://', 'http://') url = 'http://' + world_ip + '/kcsapi/' + action headers = aiohttp.MultiDict({ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Origin': 'http://' + world_ip + '/', 'Referer': referrer, }) data = yield from request.post() coro = aiohttp.post(url, data=data, headers=headers, connector=self.connector) try: response = yield from asyncio.wait_for(coro, timeout=5) except asyncio.TimeoutError: return aiohttp.web.HTTPBadRequest() body = yield from response.read() if action == 'api_start2' and len(body) > 100000: self.api_start2 = body return aiohttp.web.Response(body=body, headers=aiohttp.MultiDict({'Content-Type': 'text/plain'})) else: return aiohttp.web.HTTPBadRequest()
async def __call__(self): if hasattr(self.context, '_file_path'): with open(self.context._file_path, 'rb') as f: filename = basename(self.context._file_path) resp = StreamResponse(headers=aiohttp.MultiDict({ 'CONTENT-DISPOSITION': 'attachment; filename="%s"' % filename })) resp.content_type = mimetypes.guess_type(self.context._file_path) data = f.read() resp.content_length = len(data) await resp.prepare(self.request) resp.write(data) return resp
async def download(self): file = self.field.get(self.field.context) if file is None: raise AttributeError('No field value') resp = aiohttp.web.StreamResponse(headers=aiohttp.MultiDict({ 'CONTENT-DISPOSITION': 'attachment; filename="%s"' % file.filename })) resp.content_type = file.contentType resp.content_length = file.size await resp.prepare(self.request) resp.write(file.data) await resp.drain() return resp
def get_form_data(form): form_data = aiohttp.MultiDict() for el in form.xpath('.//input[@name]|select[@name]|textarea[@name]|button[@name]'): data = {} if el.tag == 'input': if el.attrib.get('type') == 'radio' or el.attrib.get('type') == 'checkbox': if el.attrib.get('checked', None): data[el.attrib['name']] = el.attrib.get('value', '') else: data[el.attrib['name']] = el.attrib.get('value', '') elif el.tag == 'select': options = el.xpath('./option[@selected]') if options: data[el.attrib['name']] = options[0].attrib.get('value', '') elif el.tag == 'textarea': data[el.sttrib['name']] = el.text or '' elif el.tag == 'button': if el.attrib.get('type', None) == 'submit': data[el.attrib['name']] = el.attrib.get('value', '') form_data.extend(data) return form_data
def get_flash(self, request): """获取用户的游戏FLASH地址,返回一个JSON格式的字典。 结果中`status`键值为1时获取成功,`flash_url`键值为游戏FLASH地址;`status`为0时获取失败,`message`键值提供了错误信息。 :param request: aiohttp.web.Request :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest """ data = yield from request.post() login_id = data.get('login_id', None) password = data.get('password', None) if login_id and password: headers = aiohttp.MultiDict({'Content-Type': 'application/json'}) kancolle = KancolleAuth(login_id, password) try: flash_url = yield from kancolle.get_flash() result = {'status': 1, 'flash_url': flash_url} except OOIAuthException as e: result = {'status': 0, 'message': e.message} return aiohttp.web.Response(body=json.dumps(result).encode(), headers=headers) else: return aiohttp.web.HTTPBadRequest()