Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python拦截来自浏览器的Web流量_Python_Redirect_Filtering_Forwarding - Fatal编程技术网

Python拦截来自浏览器的Web流量

Python拦截来自浏览器的Web流量,python,redirect,filtering,forwarding,Python,Redirect,Filtering,Forwarding,我正在尝试用python创建一个简单的web过滤应用程序。我想这样做的方法是监视端口tcp 80/443(http)上的流量,如果有流量,我想在让它通过之前检查一下。如果检查失败,我希望用户被重定向到我选择的页面 因此,我的问题是,当用户在浏览器中访问时,是否有一种方法可以拦截该请求,是否有一种方法可以通过我的选择将它们重定向到另一个页面?您需要编写一个web代理,并将您的web客户端代理服务器设置为(或代理正在侦听的任何内容) 然后,您的web客户端将发送HTTP,如下所示: 得到 到您的代理

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


因此,我的问题是,当用户在浏览器中访问时,是否有一种方法可以拦截该请求,是否有一种方法可以通过我的选择将它们重定向到另一个页面?

您需要编写一个web代理,并将您的web客户端代理服务器设置为(或代理正在侦听的任何内容)

然后,您的web客户端将发送HTTP,如下所示:

得到

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

得到/

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

不管怎么说,这都是标准的东西,我怀疑Python web代理已经存在,您可以使用

编辑:

这是我不久前写的一篇文章。使用webob和粘贴。TransparentProxy将请求转发到请求指定的任何url。您可以编写中间件,在请求传递给transparentproxy之前对其进行处理

然后只需将浏览器代理设置设置为代理运行的地址即可

本例打印请求和响应,对于您的情况,您希望检查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)

如果它是一个特定的网站,比如google.com,你可以把hosts文件放在其中。这将是一个丑陋但简单的解决方案

如果是go,则位于:

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

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

谢谢spacedman。因为这是一个过滤器,如果我想确保没有人禁用代理,我该怎么做?有没有办法让浏览器的默认目标保留在我的代理上?没关系。找到了我要找的--拦截proxyscapy不会执行此任务吗?请看一下我的主题: