예제 #1
0
파일: server.py 프로젝트: robin0371/mywsgi
        def write(body: bytes):
            if not headers_set:
                raise AssertionError("write() before start_response()")

            out = self.wfile
            http_headers = b""

            if not headers_sent:
                # Before the first output, send the stored headers
                status, response_headers = headers_sent[:] = headers_set
                headers_keys = [h[0] for h in response_headers]

                if "Date" not in headers_keys:
                    response_headers.append(("Date", self.date_time_string()))

                head = f"HTTP/1.1 {status}"
                headers = "".join([f"{h[0]}: {h[1]}\r\n" for h in response_headers])
                http_data = f"{head}\r\n{headers}\r\n"
                http_headers = wsgi_to_bytes("".join(http_data))

            http_result = http_headers + body
            LOGGER.debug("http response=%s", http_result)
            out.write(http_result)
            out.flush()
예제 #2
0
def get_named_param(request, param):
    return Response(wsgi_to_bytes(f"{param}"),
                    [("Content-Type", "text/plain")])
예제 #3
0
def get_json(request, param):
    return Response(wsgi_to_bytes('{"p": %s}' % param),
                    [("Content-Type", "application/json")])
예제 #4
0
 def handler(request, number):
     return Response(wsgi_to_bytes(f"{number}!"),
                     [("Content-Type", "text/plain")])