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()
def get_named_param(request, param): return Response(wsgi_to_bytes(f"{param}"), [("Content-Type", "text/plain")])
def get_json(request, param): return Response(wsgi_to_bytes('{"p": %s}' % param), [("Content-Type", "application/json")])
def handler(request, number): return Response(wsgi_to_bytes(f"{number}!"), [("Content-Type", "text/plain")])