Posts about coding (old posts, page 4)

All about coding

Python SimpleHTTPServer Recipe: Serve specific directory

  • We need to be able to pass the path to the root of the directory tree we wish to serve.
  • We can only pass arguments to the ServerClass and not HandlerClass. Note however that HandlerClass is passed as an argument to ServerClass so we should be able to propagate the argument to HandlerClass.
  • The translate_path method of SimpleHTTPRequestHandler takes the /-separated path specified in the URL and prepends os.getcwd() to it. We just have to instead prepend the argument we propagated to SimpleHTTPRequestHandler.
  • Lastly we modify the test function to take multiple optional arguments (port and base path) using the excellent module argparse.

Read more…