Python: Serving a BaseHTTPRequestHandler via WSGI

953 views Asked by At

I'm considering running a tiny Python webapp that comes as an implementation of a BaseHTTPRequestHandler (i.e. https://github.com/openid/python-openid/blob/master/examples/server.py). I want to use an existing WSGI server, i.e. Apache with mod_wsgi.

So my question is: How to conveniently serve the BaseHTTPRequestHandler via WSGI so that I can hook it up with Apache + mod_wsgi?

2

There are 2 answers

0
Graham Dumpleton On

Don't try, it would likely be one huge hack even if you tried and could somehow get it to work.

Try and find a version of your application which already works on a WSGI server.

If you need a very small WSGI server which only relies on stuff from the standard Python distribution then use the WSGI server from the wsgiref module.

If you need a simple to use WSGI framework, and try and port that application, use Flask.

0
sintrb On

If you BaseHTTPRequestHandler is very simple, you can try warp it to wsgi application, refer to SVNOnline.wsgi:

"""
wsgi.py
WSGI warp
"""

import os
import wsgiref.util

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

class HeadsWarp(object):
    environ = None
    def __init__(self, environ):
        self.environ = environ
    def getheader(self, name):
        name = name.upper().replace('-', '_')
        return self.environ.get('HTTP_%s' % (name), self.environ.get(name))

class WSGIHandler(YourHTTPRequestHandler):
    path = None
    headers = None

    response_code = 200
    response_message = 'OK'
    request_version = 'HTTP/1.0'
    response_headermap = {
                          'Content-Type':'text/html'
                          }

    def __init__(self, request, client_address, server):
        self.request = request
        self.client_address = client_address
        self.server = server
        self.setup()
        try:
            self.handle()
        finally:
            self.finish()

    def setup(self):
        pass

    def finish(self):
        pass

    def handle(self):
        pass

    def handle_one_request(self):
        pass

    def send_response(self, code, message=None):
        self.response_code = code
        self.response_message = message

    def send_header(self, keyword, value):
        self.response_headermap[keyword] = value

    @property
    def response_headers(self):
        return self.response_headermap.items()

def application(environ, start_response):    
    handler = WSGIHandler(None, (environ['REMOTE_ADDR'], None), None)
#     for k, v in environ.items():
#         print k, v
    handler.path = environ['PATH_INFO'] + ('?' + environ['QUERY_STRING'] if environ['QUERY_STRING'] else '')
    handler.rfile = environ['wsgi.input']
    handler.headers = HeadsWarp(environ)
    handler.wfile = StringIO()
    handler.request_version = environ['SERVER_PROTOCOL']
    handler.command = environ['REQUEST_METHOD']
    method = 'do_%s' % (environ['REQUEST_METHOD'])
    if hasattr(handler, method):
        handler_method = getattr(handler, method)
        handler_method()
    handler.wfile.seek(0)
    res = handler.wfile.read()[2:]
    if res.startswith('\n\n'):
        res = res[2:]
    if res:
        handler.send_header("Content-Length", str(len(res)))

    status = '%s %s' % (handler.response_code or 200, handler.response_message or 'OK')
    headers = [(k, v) for k, v in handler.response_headers if not wsgiref.util.is_hop_by_hop(k)]

    start_response(status, headers)



    return [res, ]

Then use gunicorn -b 0.0.0.0:8000 wsgi:application to test it.