【问题标题】:Python Intercept Web Traffic from BrowserPython 拦截来自浏览器的 Web 流量
【发布时间】:2025-11-25 13:20:06
【问题描述】:

我正在尝试在 python 中创建一个简单的网络过滤应用程序。我想这样做的方法是监视端口 tcp 80/443 (http) 上的流量,如果有流量,我想在让它通过之前检查一些东西。如果检查失败,我希望将用户重定向到我选择的页面。

所以我的问题是,当用户在浏览器中访问http://www.google.com 时,有没有办法可以拦截该请求,有没有办法可以通过我的选择将它们重定向到另一个页面?

【问题讨论】:

    标签: python redirect filtering forwarding


    【解决方案1】:

    您需要编写一个网络代理,并将您的网络客户端代理服务器设置为http://localhost:8000/(或代理正在侦听的任何内容)。

    然后,您的 Web 客户端将像这样发送 HTTP:

    获取http://www.google.com

    到您的代理,然后它必须重写为:

    获取/

    然后发送到 www.google.com,获取响应,然后通过原始套接字将其发送回客户端。请注意,解释被大大简化了。

    无论如何,它都是标准的东西,我怀疑 Python 网络代理已经存在,供你破解。

    编辑:http://proxies.xhaus.com/python/

    【讨论】:

    • 感谢 spacedman。由于这是一个过滤器,如果我想确保没有人禁用代理,我该怎么做?有没有办法让浏览器的默认目标留在我的代理中?
    • 没关系。找到了我要找的东西——拦截代理
    • scapy 不会做这个任务?请看我的话题:*.com/questions/9774525/…
    【解决方案2】:

    这是来自我不久前写的blog post。使用 webob 和粘贴。 TransparentProxy 将请求转发到请求指定的任何 url。您可以编写中间件来处理请求,然后再将其交给透明代理。

    然后只需将您的浏览器代理设置设置为您的代理运行的任何地址。

    此示例打印请求和响应,对于您的情况,您想要检查 404 或 302 或其他任何内容的响应状态并发送到您编写的代码。

    from webob.dec import wsgify
    from paste import httpserver
    from paste.proxy import TransparentProxy
    
    
    def print_trip(request, response):
        """
        just prints the request and response
        """
        print "Request\n==========\n\n"
        print str(request)
        print "\n\n"
        print "Response\n==========\n\n"
        print str(response)
        print "\n\n"
    
    
    class HTTPMiddleware(object):
        """
        serializes every request and response
        """
    
        def __init__(self, app, record_func=print_trip):
            self._app = app
            self._record = record_func
    
        @wsgify
        def __call__(self, req):
            result = req.get_response(self._app)
            try:
                self._record(req.copy(), result.copy())
            except Exception, ex: #return response at all costs
                print ex
            return result
    
    httpserver.serve(HTTPMiddleware(TransparentProxy()), "0.0.0.0", port=8088)
    

    编辑:

    这是我编写的中间件示例,因此我可以截取路径并返回不同的响应。我用它来测试一个为生产而硬编码的 javascript 繁重的应用程序,我截取 config.js 并输出我自己的,它具有 unittest 特定的设置。

    class FileIntercept(object):
        """
        wsgi: middleware
        given request.path will call wsgi app matching that path instead
        of dispatching to the wrapped application
        """
        def __init__(self, app, file_intercept={}):
            self._app = app
            self._f = file_intercept
    
        def __call__(self, environ, start_response):
            request = Request(environ)
            if request.path.lower() in self._f:
                response = request.get_response(self._f[request.path.lower()])
            else:
                response = request.get_response(self._app)
            return response(environ, start_response)
    

    作为一个例子,我会像这样初始化它......

     app = FileIntercept(TransparentProxy(),
                                 file_intercept={"/js/config.js":Response("/*new settings*/")})
     httpserver.serve(HTTPMiddleware(app), "0.0.0.0", port=8088)
    

    【讨论】:

      【解决方案3】:

      如果它是一个特定的网站,例如 google.com,您可以随时破坏 hosts 文件。这将是一个丑陋但简单的解决方案。

      如果可以的话,它位于:

      C:/windows/system32/drivers/hosts.txt
      

      它也在 linux 上的 etc 中,但不确定...

      【讨论】: