
import http.server
import sys
from argparse import ArgumentParser



class RequestHandler(http.server.BaseHTTPRequestHandler):

    def do_GET(self):
        print(self.requestline)
        for (key, header) in self.headers.items():
            print("{} : {}".format(key, header))
        print("---")

        self.send_response(200, "OK")
        self.end_headers()
        self.wfile.write(b"Thanks for all the fish...")



def getArguments():
    """
    Get command line arguments.
    """

    description = "Simple HTTP server for testing."
    epilog = '''
    '''
    argParser = ArgumentParser(description=description, epilog=epilog, fromfile_prefix_chars='@')
    argParser.add_argument('port', type=int, nargs='?', help='Server port')
    return argParser.parse_args()


def main():
    args = getArguments()
    port = args.port

    print("Starting HTTP server on port {}".format(port))
    serverAddress = ("", port)
    server = http.server.HTTPServer(serverAddress , RequestHandler)
    server.serve_forever()




if __name__ == '__main__':
    main()


import http.server
import json
import sys
from argparse import ArgumentParser



class RequestHandler(http.server.BaseHTTPRequestHandler):

    # Handle GET requests
    def do_GET(self):
        response = {}

        # Capture the URL request
        response["request"] = self.requestline

        # Capture all header fields
        headers = {}
        for (key, header) in self.headers.items():
            headers[key] = header
        response["headers"] = headers

        # Return the response
        self.send_response(200, "OK")
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(json.dumps(response, sort_keys=True, indent=4).encode("utf-8"))


def getArguments():
    """
    Get command line arguments.
    """

    description = "Simple HTTP server for testing."
    epilog = '''
    '''
    argParser = ArgumentParser(description=description, epilog=epilog, fromfile_prefix_chars='@')
    argParser.add_argument('port', type=int, nargs='?', help='Server port')
    return argParser.parse_args()


def main():
    args = getArguments()
    port = args.port

    print("Starting HTTP server on port {}".format(port))
    serverAddress = ("", port)
    server = http.server.HTTPServer(serverAddress , RequestHandler)
    server.serve_forever()




if __name__ == '__main__':
    main()

Send back GET request in response
Send back GET request in response

The response to a GET request now returns the request string
and all header fields as a JSON encoded response.

  
  import http.server
+ import json
  import sys
  from argparse import ArgumentParser
  
  
  
  class RequestHandler(http.server.BaseHTTPRequestHandler):
  
+     # Handle GET requests
      def do_GET(self):
-         print(self.requestline)
+         response = {}
+ 
+         # Capture the URL request
+         response["request"] = self.requestline
+ 
+         # Capture all header fields
+         headers = {}
          for (key, header) in self.headers.items():
-             print("{} : {}".format(key, header))
-         print("---")
+             headers[key] = header
+         response["headers"] = headers
  
+         # Return the response
          self.send_response(200, "OK")
+         self.send_header("Content-Type", "application/json")
          self.end_headers()
+         self.wfile.write(json.dumps(response, sort_keys=True, indent=4).encode("utf-8"))
-         self.wfile.write(b"Thanks for all the fish...")
- 
  
  
  def getArguments():
      """
      Get command line arguments.
      """
  
      description = "Simple HTTP server for testing."
      epilog = '''
      '''
      argParser = ArgumentParser(description=description, epilog=epilog, fromfile_prefix_chars='@')
      argParser.add_argument('port', type=int, nargs='?', help='Server port')
      return argParser.parse_args()
  
  
  def main():
      args = getArguments()
      port = args.port
  
      print("Starting HTTP server on port {}".format(port))
      serverAddress = ("", port)
      server = http.server.HTTPServer(serverAddress , RequestHandler)
      server.serve_forever()
  
  
  
  
  if __name__ == '__main__':
      main()
  
Send back GET request in response
## Code Before:

import http.server
import sys
from argparse import ArgumentParser



class RequestHandler(http.server.BaseHTTPRequestHandler):

    def do_GET(self):
        print(self.requestline)
        for (key, header) in self.headers.items():
            print("{} : {}".format(key, header))
        print("---")

        self.send_response(200, "OK")
        self.end_headers()
        self.wfile.write(b"Thanks for all the fish...")



def getArguments():
    """
    Get command line arguments.
    """

    description = "Simple HTTP server for testing."
    epilog = '''
    '''
    argParser = ArgumentParser(description=description, epilog=epilog, fromfile_prefix_chars='@')
    argParser.add_argument('port', type=int, nargs='?', help='Server port')
    return argParser.parse_args()


def main():
    args = getArguments()
    port = args.port

    print("Starting HTTP server on port {}".format(port))
    serverAddress = ("", port)
    server = http.server.HTTPServer(serverAddress , RequestHandler)
    server.serve_forever()




if __name__ == '__main__':
    main()

## Instruction:
Send back GET request in response
## Code After:

import http.server
import json
import sys
from argparse import ArgumentParser



class RequestHandler(http.server.BaseHTTPRequestHandler):

    # Handle GET requests
    def do_GET(self):
        response = {}

        # Capture the URL request
        response["request"] = self.requestline

        # Capture all header fields
        headers = {}
        for (key, header) in self.headers.items():
            headers[key] = header
        response["headers"] = headers

        # Return the response
        self.send_response(200, "OK")
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(json.dumps(response, sort_keys=True, indent=4).encode("utf-8"))


def getArguments():
    """
    Get command line arguments.
    """

    description = "Simple HTTP server for testing."
    epilog = '''
    '''
    argParser = ArgumentParser(description=description, epilog=epilog, fromfile_prefix_chars='@')
    argParser.add_argument('port', type=int, nargs='?', help='Server port')
    return argParser.parse_args()


def main():
    args = getArguments()
    port = args.port

    print("Starting HTTP server on port {}".format(port))
    serverAddress = ("", port)
    server = http.server.HTTPServer(serverAddress , RequestHandler)
    server.serve_forever()




if __name__ == '__main__':
    main()
