【问题标题】:Forward a copy of http requests to another (test) environment将 http 请求的副本转发到另一个(测试)环境
【发布时间】:2011-01-23 01:44:07
【问题描述】:

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

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

【问题讨论】:

  • 所以每次有人访问您的生产站点时,您想将该请求转发到您的测试站点吗?我很好奇,为什么?
  • 是的。我想将新功能部署到测试环境,在实际将它们部署到生产环境之前,我可以在其中获取有关它们如何处理生产数据的指标。我认为我将尝试在为我的应用程序提供服务的 wsgi 脚本中执行此操作,看看效果如何。我会尝试几件事,然后在这里发布关于哪些有效(或无效)的信息。
  • 你最终知道如何做到这一点了吗?

标签: django deployment production-environment duplicate-data test-environments


【解决方案1】:

我设法像这样转发请求

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

为了代码重用,考虑装饰器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2013-11-08
    • 2017-05-20
    相关资源
    最近更新 更多