Sam*_*Sam 10 python redirect filtering forwarding
我正在尝试在python中创建一个简单的Web过滤应用程序.我想这样做的方法是监控端口tcp 80/443(http)上的流量,如果有流量,我想在检查之前检查一下.如果检查失败,我希望将用户重定向到我选择的页面.
所以我的问题是,当用户在浏览器中访问http://www.google.com时,是否有一种方法可以拦截该请求,是否可以通过我的选择将其重定向到另一个页面?
您需要编写Web代理,并将Web客户端代理服务器设置为http:// localhost:8000 /(或代理正在侦听的任何内容).
然后,您的Web客户端将发送HTTP,如下所示:
到你的代理,它必须重写为:
GET /
并发送到www.google.com,获取响应,然后将原始套接字上的回复发送给客户端.请注意,大大简化了解释.
无论如何,它的所有标准的东西,我怀疑Python网络代理已存在,你可以破解.
编辑:http://proxies.xhaus.com/python/
这是我不久前写的一篇博客文章。使用 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)
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我编写的中间件示例,因此我可以拦截路径并返回不同的响应。我用它来测试一个针对生产进行硬编码的 javascript 重型应用程序,我拦截 config.js 并输出我自己的具有单元测试特定设置的应用程序。
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)
Run Code Online (Sandbox Code Playgroud)
作为一个例子,我会像这样初始化它......
app = FileIntercept(TransparentProxy(),
file_intercept={"/js/config.js":Response("/*new settings*/")})
httpserver.serve(HTTPMiddleware(app), "0.0.0.0", port=8088)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9092 次 |
最近记录: |