5

I want all the production data for my web-app to also flow through my testing environment. Essentially, I want to forward every http request for the production site to the test site (and also have the production website serve it!).

What is a good way to do this? My site is built with Django, and served by mod_wsgi. Is this best implemented at the app-level (Django), web server level (Apache), or the mod_wsgi-level?

raviv
  • 205
  • 4
  • 9
  • So every time someone hits your production site, you want to forward that request to your test site? I'm curious, why? – Dominic Rodger Mar 03 '10 at 07:54
  • Yes. I want to deploy new features to the test environment, where I can get metrics on how they perform with production data before actually deploying them to production. I *think* I'm going to try to do this in the wsgi script that serves my app, and see how that goes. I'll try out a couple things, and post back here about what works (or doesn't). – raviv Mar 05 '10 at 08:24
  • Did you eventually figure out how to do this? – André Caron Jan 16 '11 at 17:18

1 Answers1

7

I managed to forward request like this

def view(request):
    # do what you planned to do here
    ...

    # processing headers
    def format_header_name(name):
        return "-".join([ x[0].upper()+x[1:] for x in name[5:].lower().split("_") ])
    headers = dict([ (format_header_name(k),v) for k,v in request.META.items() if k.startswith("HTTP_") ])
    headers["Cookie"] = "; ".join([ k+"="+v for k,v in request.COOKIES.items()])

    # this conversion is needed to avoid http://bugs.python.org/issue12398
    url = str(request.get_full_path())

    # forward the request to SERVER_DOMAIN
    conn = httplib.HTTPConnection("SERVER_DOMAIN")
    conn.request(
        request.method,
        url,
        request.raw_post_data,
        headers
    )
    response = conn.getresponse()

    # some error handling if needed
    if response.status != 200:
        ...

    # render web page as usual
    return render_to_response(...)

For code reuse, consider decorators

observerss
  • 71
  • 1
  • 2