开发者

Forward a copy of http requests to another (test) environment

开发者 https://www.devze.com 2022-12-22 04:53 出处:网络
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

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 Djan开发者_Python百科go, and served by mod_wsgi. Is this best implemented at the app-level (Django), web server level (Apache), or the mod_wsgi-level?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消