Пример #1
0
    def render_json(self, code, data, msg='', status_code=200, set_cookie=None):
        writelog(request.remote_addr + ' render_json:' + str(data) + '\n')
        try:
            # python默认参数是不能为字典或者列表的,要像下面这样给默认值(直接set_cookie={}是错的)。
            if set_cookie is None:
                set_cookie = {}

            exec_time = time.time() - self.request_start_time
            data = {'code': code, 'time': exec_time, 'data': data, 'msg': msg}

            response = make_response(json.dumps(data))
            response.headers['Content-Type'] = 'application/json;charset=utf-8'
            response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
            response.headers['Access-Control-Request-Method'] = 'POST, GET'
            response.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*')

            if request.headers.get('Origin', ''):
                response.headers['Access-Control-Allow-Credentials'] = 'true'

            if set_cookie:
                max_age = set_cookie['max_age']
                cookies = set_cookie['cookies']
                for key in cookies:
                    response.set_cookie(key, cookies[key], max_age=max_age)

            return response, status_code
        except:
            writelog(traceback.format_exc())
Пример #2
0
    def render_file(self,
                    out_file,
                    status_code=200,
                    headers=None,
                    auto_delete=False):
        if headers is None:
            headers = {}

        writelog(request.remote_addr + ' render_file:' + str(out_file) + '\n')
        try:
            fp = open(out_file, 'rb')
            response = make_response(fp.read())
            response.headers[
                "Content-Disposition"] = "attachment; filename=" + out_file.split(
                    os.path.sep)[-1]
            fp.close()

            response.headers[
                'Access-Control-Request-Method'] = 'POST, GET, OPTIONS'
            response.headers['Access-Control-Allow-Origin'] = headers.get(
                'Origin', '*')

            if headers.get('Origin', ''):
                response.headers['Access-Control-Allow-Credentials'] = 'true'

            if auto_delete and os.path.exists(out_file):
                os.remove(out_file)

            return response, status_code
        except:
            writelog(traceback.format_exc())
Пример #3
0
    def render_xml(self, root, data, status_code=200):
        writelog(request.remote_addr + ' rend_xml:' + str(data) + '\n')
        try:
            response = make_response(self.__dict2xmlstring(root, data))
            response.headers['Content-Type'] = 'application/xml'

            return response, status_code
        except:
            writelog(traceback.format_exc())
Пример #4
0
    def render_txt(self, data, status_code=200):
        writelog(request.remote_addr + ' render_txt:' + str(data) + '\n')
        try:
            response = make_response(data)
            response.headers['Content-Type'] = 'text/plain'

            return response, status_code
        except:
            writelog(traceback.format_exc())
Пример #5
0
    def before_request(self, name, **kwargs):
        try:
            # 把每次请求记录,有迹可循
            content = 'remote_addr:%s, method:%s, url:%s, args:%s, data:%s' %\
                      (request.remote_addr, request.method, request.url, request.args, request.data)
            writelog(content)

            # 获取参数
            self.init_params()

            # 渲染器
            self._render = Render()

        except:
            writelog(traceback.format_exc())
Пример #6
0
    def before_request(self, name, **kwargs):
        try:
            #把每次请求记录,有迹可循
            content = 'method:' + request.method + ',' + 'remote_addr:' + request.remote_addr + ',' + 'url:' + request.url + '\n' + \
                      'data:' + str(request.data) + ',' + 'args:' + str(request.args) + ',' + 'form:' + str(request.form) + ',' + 'json:' + str(request.json)

            writelog(content)

            #获取参数
            self.init_params()

            #渲染器
            self._render = Render()
        except:
            writelog(traceback.format_exc())
Пример #7
0
    def rend_template(self, template, set_cookie=None, status_code=200, **kwargs):
        writelog(request.remote_addr + ' rend_template:' + template + '\n')
        try:
            if set_cookie is None:
                set_cookie = {}

            response = make_response(render_template(template, **kwargs))
            response.headers['Content-Type'] = 'text/html; charset=utf-8'

            if set_cookie:
                for key in set_cookie['cookies']:
                    response.set_cookie(key, set_cookie['cookies'][key], max_age=set_cookie['max_age'])

            return response, status_code
        except:
            writelog(traceback.format_exc())
Пример #8
0
    def login(self):
        code = '11000'
        data = {}
        msg = '登陆失败,请配置好你的数据库,在login_controller补上登陆逻辑后测试.'

        try:
            phone = self.params.get('phone', '')
            password = self.params.get('password', '')

            controller = LoginController()
            result = controller.login(phone=phone, password=password)

            if result.get('token', ''):
                code = '10000'
                data = result
                msg = 'success'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)
Пример #9
0
    def render_json(self,
                    code,
                    data,
                    msg='',
                    status_code=200,
                    set_cookie=None,
                    headers=None):
        writelog(request.remote_addr + ' render_json:' + str(data) + '\n')
        try:
            if set_cookie is None:
                set_cookie = {}

            if headers is None:
                headers = {}

            exec_time = time.time() - self.request_start_time
            data = {'code': code, 'time': exec_time, 'data': data, 'msg': msg}

            response = make_response(json.dumps(data))
            response.headers['Content-Type'] = 'application/json'
            response.headers[
                'Access-Control-Request-Method'] = 'POST, GET, OPTIONS'
            response.headers['Access-Control-Allow-Origin'] = headers.get(
                'Origin', '*')

            if headers.get('Origin', ''):
                response.headers['Access-Control-Allow-Credentials'] = 'true'

            if set_cookie:
                max_age = set_cookie['max_age']
                cookies = set_cookie['cookies']
                for key in cookies:
                    response.set_cookie(key, cookies[key], max_age=max_age)

            return response, status_code
        except:
            writelog(traceback.format_exc())