I have the following mockup server which works fine except one item. We are trying to send any header items that is coming with an HTTP request from the client back to the client as header items.
The code works perfectly when we try it with HEAD method or if we change the response code for GET method to 204. But when we try to use the get method with response code 200 , which will send both the body and the header, The system does not complete the process. For some reason it keeps working and do not complete the request. It sends the header, sends the msg body , but then instead of finishing the task it keeps the task going on, but we have no idea what is going one except that the server becomes unresponsive.
We will appreciate if someone can provide a solution to overcome this.
Thanks
from http.server import HTTPServer, BaseHTTPRequestHandler import ssl, sys, time, os, datetime class Mock(BaseHTTPRequestHandler): def do_GET(self): print(" GET: Headers: {}".format(self.headers)) sys.stdout.flush() self.send_response(200) # captures the header in original request, converts it in a dictionary item # and then sends the items back as headers a = dict(self.headers) for key, value in a.items(): self.send_header(key, value) self.end_headers() f=open(rpath,"r") self.wfile.write(f.read().encode()) f.close() def do_HEAD(self): self.send_response(204) a = dict(self.headers) print(a) for key, value in a.items(): self.send_header(key, value) self.end_headers() def main(): global hostname, port hostname = "127.0.0.1" port = 8000 myServer = HTTPServer((hostname, port), Mock) myServer.serve_forever() if __name__ =="__main__": main()