Esempio n. 1
0
def test():
    with open("./example_result.html", "r") as f:
        example_result = f.read()
    title = "test case"
    obj = Obj()
    numbers = [1, 2, 3, 4, 5, 6]
    dictionary = {"dict": "Tis is dict"}
    render_result = render("./example.html", title=title, obj=obj, numbers=numbers, dict=dictionary)
    render_result = render_result.replace(" ", "").replace("\t", "").replace("  ", "").replace("\n", "")
    render_result = utf8(render_result)
    example_result = example_result.replace(" ", "").replace("\t", "").replace("  ", "").replace("\n", "")
    if render_result != example_result:
        print type(example_result), type(render_result)
        print example_result
        print render_result
        assert "render error"
    with open("./example.html", "r") as f:
        example_html = f.read()
    render_result = render_string(example_html, title=title, obj=obj, numbers=numbers, dict=dictionary)
    render_result = render_result.replace(" ", "").replace("\t", "").replace("  ", "").replace("\n", "")
    render_result = utf8(render_result)
    example_result = example_result.replace(" ", "").replace("\t", "").replace("  ", "").replace("\n", "")
    if render_result != example_result:
        print type(example_result), type(render_result)
        print example_result
        print render_result
        assert "render_string error"
Esempio n. 2
0
    def response_as_json(self, data):
        """

        :param data: the response data
        :return: json data

        """
        self.set_header({"Content-Type": "application/json"})
        response = json.dumps(data, ensure_ascii=False)
        return utf8(response)
Esempio n. 3
0
    def response_as_json(self, data):
        """decorate data to http json data

        :param data: the response data
        :return: json data

        """
        self.set_header({"Content-Type": "application/json"})
        response = json.dumps(data, ensure_ascii=False)
        return utf8(response), HttpStatus.SUCCESS, HttpStatusMsg.SUCCESS, self
Esempio n. 4
0
    def response_as_json(self, data):
        """

        :param data: the response data
        :return: json data

        """
        self.set_header({"Content-Type": "application/json"})
        response = json.dumps(data, ensure_ascii=False)
        return utf8(response)
Esempio n. 5
0
    def response_as_json(self, data, ensure_gzip=False):
        """decorate data to http json data

        :param data: the response data
        :param ensure_gzip: whether compress the response data use gzip
        :return: json data

        """
        self.set_header({"Content-Type": "application/json"})
        response = json.dumps(data, ensure_ascii=False)
        if ensure_gzip and not PY3:
            out = io.BytesIO()
            with gzip.GzipFile(fileobj=out, mode="w") as f:
                f.write(response)
            response = out.getvalue()
            self.set_header({"Content-Encoding": "gzip"})
            response = utf8(response)
        return response, HttpStatus.SUCCESS, HttpStatusMsg.SUCCESS, self
Esempio n. 6
0
    def response_as_json(self, data, ensure_gzip=False):
        """decorate data to http json data

        :param data: the response data
        :param ensure_gzip: whether compress the response data use gzip
        :return: json data

        """
        self.set_header({"Content-Type": "application/json"})
        response = json.dumps(data, ensure_ascii=False)
        if ensure_gzip and not PY3:
            out = io.BytesIO()
            with gzip.GzipFile(fileobj=out, mode="w") as f:
                f.write(response)
            response = out.getvalue()
            self.set_header({"Content-Encoding": "gzip"})
            response = utf8(response)
        return response, HttpStatus.SUCCESS, HttpStatusMsg.SUCCESS, self
Esempio n. 7
0
    def __parse_data(self):
        """

        parse http request data to dict and get handler's response data

        :return: http response data

        """
        http_message = self.data.split("\r\n\r\n")
        http_request_head = http_message[0]
        http_request_line = http_request_head.split("\r\n")[0].split(" ")
        http_method = http_request_line[0].lower()
        http_url = http_request_line[1]
        http_version = http_request_line[2]
        http_head_list = http_request_head.split("\r\n")[1:]
        if "" in http_head_list:
            http_head_list.remove("")
        header = dict((header.split(": ")[0].lower(), header.split(": ")[1])
                      for header in http_head_list)
        http_body = http_message[1] if len(http_message) > 1 else None
        http_message_dict = dict({})
        http_message_dict["url"] = http_url
        http_message_dict["method"] = http_method
        http_message_dict["version"] = http_version
        http_message_dict["header"] = header
        http_message_dict["body"] = http_body
        now = datetime.datetime.now()
        now_time = now.strftime("%a, %d %b %Y %H:%M:%S GMT")
        status = dict({})
        status["date"] = now_time
        status["host"] = self.host
        url = http_url.split("?")[0]
        handler = Router(self.handlers, url).get_handler()
        path_param = handler[1]
        handler = handler[0]
        if not handler:
            data = HTTPResponse404(**self.settings).data()
            status["status"] = HTTPResponse404.status
            status["content_type"] = ContentType.HTML
            status["content_length"] = len(utf8(data))
            status["status_msg"] = HTTPResponse404.message
        else:
            try:
                handler_init = handler(http_message_dict, self.data,
                                       self.settings)
                if path_param.status:
                    handler_init.set_path_param(path_param.value)
                handler_init.before_request()
                pro_function = getattr(handler_init, http_method)
                data = pro_function()
                if isinstance(data, type):
                    return data
                try:
                    handler_init_k = data[-1]
                    http_response_header = handler_init_k.get_response_header()
                except Exception as e:
                    self.logger.info("static handler response: %s", str(e))
                    http_response_header = handler_init.get_response_header()
                self.response_header = self.response_header.replace(
                    "\r\n\r\n", http_response_header)
                status["status"] = data[1]
                status["status_msg"] = data[2]
                data = data[0]
                handler_init.teardown_request()
            except Exception as e:
                status["status"] = HTTPResponse500.status
                status["status_msg"] = HTTPResponse500.message
                data = HTTPResponse500(**self.settings).data() or str(e)
            status["content_length"] = len(utf8(data))
        response_data = self.response_header % status + data
        return response_data
Esempio n. 8
0
    def __parse_data(self):
        """

        parse http request data to dict and get handler's response data

        :return: http response data

        """
        http_message = self.data.split("\r\n\r\n")
        http_request_head = http_message[0]
        http_request_line = http_request_head.split("\r\n")[0].split(" ")
        http_method = http_request_line[0].lower()
        http_url = http_request_line[1]
        http_version = http_request_line[2]
        http_head_list = http_request_head.split("\r\n")[1:]
        if "" in http_head_list:
            http_head_list.remove("")
        header = dict((header.split(": ")[0].lower(), header.split(": ")[1]) for header in http_head_list)
        http_body = http_message[1] if len(http_message) > 1 else None
        http_message_dict = dict({})
        http_message_dict["url"] = http_url
        http_message_dict["method"] = http_method
        http_message_dict["version"] = http_version
        http_message_dict["header"] = header
        http_message_dict["body"] = http_body
        now = datetime.datetime.now()
        now_time = now.strftime("%a, %d %b %Y %H:%M:%S GMT")
        status = dict({})
        status["date"] = now_time
        status["host"] = self.host
        url = http_url.split("?")[0]
        handler = Router(self.handlers, url).get_handler()
        path_param = handler[1]
        handler = handler[0]
        if not handler:
            data = HTTPResponse404(**self.settings).data()
            status["status"] = HTTPResponse404.status
            status["content_type"] = ContentType.HTML
            status["content_length"] = len(utf8(data))
            status["status_msg"] = HTTPResponse404.message
        else:
            try:
                handler_init = handler(http_message_dict, self.data, self.settings)
                if path_param.status:
                    handler_init.set_path_param(path_param.value)
                handler_init.before_request()
                pro_function = getattr(handler_init, http_method)
                data = pro_function()
                if isinstance(data, type):
                    return data
                try:
                    handler_init_k = data[-1]
                    http_response_header = handler_init_k.get_response_header()
                except Exception as e:
                    self.logger.info("static handler response: %s", str(e))
                    http_response_header = handler_init.get_response_header()
                self.response_header = self.response_header.replace("\r\n\r\n", http_response_header)
                status["status"] = data[1]
                status["status_msg"] = data[2]
                data = data[0]
                handler_init.teardown_request()
            except Exception as e:
                status["status"] = HTTPResponse500.status
                status["status_msg"] = HTTPResponse500.message
                data = HTTPResponse500(**self.settings).data() or str(e)
            status["content_length"] = len(utf8(data))
        response_data = self.response_header % status + data
        return response_data