Differences between BaseHttpServer and wsgiref.simple_server
Question: I'm looking for a module that provides me a basic http server capabilities for
local access. It seems like Python has two methods to implement simple http
servers in the standard library:
[wsgiref.simple_server](https://docs.python.org/2/library/wsgiref.html#module-
wsgiref.simple_server) and
[BaseHttpServer](https://docs.python.org/2/library/basehttpserver.html).

What are the differences? Is there any strong reason to prefer one over the
other?


Answer: **Short answer:** `wsgiref.simple_server` is a WSGI adapter over
`BaseHTTPServer`.

**Longer answer:**

`BaseHTTPServer` is the module that actually implements the HTTP server part.
It can accept requests and return responses, but it has to know how to handle
those requests. When you are using pure `BaseHTTPServer`, you provide the
handlers by subclassing
[`BaseHTTPRequestHandler`](https://docs.python.org/2/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler),
for example:

    
    
    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
    
    class MyHandler(BaseHTTPRequestHandler):
    
        def do_GET(self):
            self.send_response(200)
            self.send_header('Content-Type', 'text/plain')
            self.end_headers()
            self.wfile.write('Hello world!\n')
    
    HTTPServer(('', 8000), MyHandler).serve_forever()
    

`wsgiref.simple_server` adapts this `BaseHTTPServer` interface to the
[WSGI](http://wsgi.readthedocs.org/en/latest/) specification, which is the
standard for server-independent Python web applications. In WSGI, you provide
the handler in the form of a function, for example:

    
    
    from wsgiref.simple_server import make_server
    
    def my_app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        yield 'Hello world!\n'
    
    make_server('', 8000, my_app).serve_forever()
    

The `make_server` function returns an instance of `BaseHTTPServer.HTTPServer`.
This means that the underlying HTTP logic is the same as in `BaseHTTPServer`.
What differs is the way you integrate your application code with that logic.

It really depends on the problem you’re trying to solve, but coding against
`wsgiref` is probably a better idea because it will make it easy for you to
move to a different, production-grade HTTP server, such as
[uWSGI](https://uwsgi-docs.readthedocs.org/en/latest/) or
[Gunicorn](http://gunicorn.org/), in the future.

