Base Http Server configuration


I gave a shot for PythonAnywhere and it seems promising but I'm facing issue with starting my BaseHttpServer, which works fine on localhost.

I'm trying to start server like this:

self._server = HTTPServer(("localhost", 8080), RequestHandler)

Is it proper way ? I don;t know the concept of WSGI, should I implement following method ?

def application(environ, start_response):
    if environ.get('PATH_INFO') == '/':
        status = '200 OK'
        content = 'Server is up'

How start BaseHttpServer with WSGI ? I don't want to make a big refactor because it works fine on localhost .. I have also my RequestHandler:

class RequestHandler(BaseHTTPRequestHandler)

Unfortunately the Python HTTPServer and WSGI work in different ways, so you'd need to port your code over from one to the other.

The HTTPServer runs as a process, which opens a server socket to handle incoming connections, and then delegates the handling of the requests that come in on those connections to your code. Your Python code starts the HTTPServer (and thus, indirectly creates the server socket) itself. So your web server program is completely self-contained.

A WSGI server is a separate process (often written in a different language, eg. C) that handles all incoming connections itself, and then calls Python code to handle the requests. In effect, what you're doing when writing a WSGI-based web app is writing a "plug-in" that the WSGI server will use to handle requests.

The reason most Python websites use WSGI these days is that safely handling incoming HTTP connections is surprisingly hard when it's on the public internet, due to security and scaling concerns. The Python HTTPServer library isn't really written to address all of that. Better to hand it off to a specialised bit of code so that you can focus on the logic that makes your website special.

PythonAnywhere works by providing a WSGI server (specifically, we use uWSGI) -- so you just provide a Python module that exports a WSGI application, which uWSGI can then use as the "plug-in". This is really pretty easy if you're using a Python web framework like Flask, Django, web2py or Bottle, as they abstract all the details of WSGI away and provide you with a simple way to define URLs and views to handle them. But converting your HTTPServer over to raw WSGI would take a while.