将 http 请求的副本转发到另一个(测试)环境

发布于 2024-08-23 18:08:55 字数 199 浏览 3 评论 0原文

我希望我的网络应用程序的所有生产数据也能流经我的测试环境。本质上,我想将生产站点的每个 http 请求转发到测试站点(并且还让生产网站为其提供服务!)。

有什么好的方法可以做到这一点?我的网站是用 Django 构建的,并由 mod_wsgi 提供服务。这最好是在应用程序级别 (Django)、Web 服务器级别 (Apache) 还是 mod_wsgi 级别实现?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-08-30 18:08:55

我设法像这样转发请求

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(...)

对于代码重用,请考虑装饰器

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文