'python http server: Get response headers as the would be sent
When using the code as below, the httpServer automatically adds some headers to the outgoing HTTP response (such as the 'Host' header). How can I get access to all the headers that are auto-generated and sent to the client?
from http.server import BaseHTTPRequestHandler, HTTPServer
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("Hello World", "utf-8"))
__want to read all response.headers here as the would be sent to the client... self.response.headers doesn't contain them, they are not added till this method returns?...___
webServer = HTTPServer((hostName, serverPort), MyServer)
webServer.serve_forever()
Solution 1:[1]
The Host header is a request header. You can't see it in an HTTP response.
In Python 3.8, self._headers_buffer is a list which contains the HTTP response status line as well as response headers only after you've called self.send_response and before you call either self.end_headers or self.flush_headers.
The buffer is reset to an empty list after either of those calls.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
