Python - 扭曲、代理和修改内容 [英] Python - Twisted, Proxy and modifying content

查看:72
本文介绍了Python - 扭曲、代理和修改内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我查看了一些涉及使用 python 和 Twisted 框架编写 HTTP 代理的内容.

So i've looked around at a few things involving writting an HTTP Proxy using python and the Twisted framework.

本质上,就像其他一些问题一样,我希望能够修改将发送回浏览器的数据.也就是说,浏览器请求一个资源,代理将获取它.在资源返回到浏览器之前,我希望能够修改任何(HTTP 标头和内容)内容.

Essentially, like some other questions, I'd like to be able to modify the data that will be sent back to the browser. That is, the browser requests a resource and the proxy will fetch it. Before the resource is returned to the browser, i'd like to be able to modify ANY (HTTP headers AND content) content.

这(需要帮助编写一个扭曲的代理)是我最初的想法成立.我试过了,但它对我不起作用.我也发现了这个( Python Twisted 代理 - 如何拦截数据包)我认为这可行,但是我只能看到来自浏览器的 HTTP 请求.

This ( Need help writing a twisted proxy ) was what I initially found. I tried it out, but it didn't work for me. I also found this ( Python Twisted proxy - how to intercept packets ) which i thought would work, however I can only see the HTTP requests from the browser.

我正在寻找任何建议.我的一些想法是使用 ProxyClient 和 ProxyRequest 类并覆盖这些函数,但我读到 Proxy 类本身是两者的组合.

I am looking for any advice. Some thoughts I have are to use the ProxyClient and ProxyRequest classes and override the functions, but I read that the Proxy class itself is a combination of the both.

对于那些可能要求查看一些代码的人,应该注意的是,我只使用了上述两个示例.任何帮助都很棒.

For those who may ask to see some code, it should be noted that I have worked with only the above two examples. Any help is great.

谢谢.

推荐答案

创建ProxyFactory,可以修改服务器响应头,内容可以覆盖ProxyClient.handle*() 方法:

To create ProxyFactory that can modify server response headers, content you could override ProxyClient.handle*() methods:

from twisted.python import log
from twisted.web import http, proxy

class ProxyClient(proxy.ProxyClient):
    """Mangle returned header, content here.

    Use `self.father` methods to modify request directly.
    """
    def handleHeader(self, key, value):
        # change response header here
        log.msg("Header: %s: %s" % (key, value))
        proxy.ProxyClient.handleHeader(self, key, value)

    def handleResponsePart(self, buffer):
        # change response part here
        log.msg("Content: %s" % (buffer[:50],))
        # make all content upper case
        proxy.ProxyClient.handleResponsePart(self, buffer.upper())

class ProxyClientFactory(proxy.ProxyClientFactory):
    protocol = ProxyClient

class ProxyRequest(proxy.ProxyRequest):
    protocols = dict(http=ProxyClientFactory)

class Proxy(proxy.Proxy):
    requestFactory = ProxyRequest

class ProxyFactory(http.HTTPFactory):
    protocol = Proxy

我通过查看twisted.web.proxy.我不知道这是多么地道.

I've got this solution by looking at the source of twisted.web.proxy. I don't know how idiomatic it is.

要将其作为脚本或通过 twistd 运行,请在末尾添加:

To run it as a script or via twistd, add at the end:

portstr = "tcp:8080:interface=localhost" # serve on localhost:8080

if __name__ == '__main__': # $ python proxy_modify_request.py
    import sys
    from twisted.internet import endpoints, reactor

    def shutdown(reason, reactor, stopping=[]):
        """Stop the reactor."""
        if stopping: return
        stopping.append(True)
        if reason:
            log.msg(reason.value)
        reactor.callWhenRunning(reactor.stop)

    log.startLogging(sys.stdout)
    endpoint = endpoints.serverFromString(reactor, portstr)
    d = endpoint.listen(ProxyFactory())
    d.addErrback(shutdown, reactor)
    reactor.run()
else: # $ twistd -ny proxy_modify_request.py
    from twisted.application import service, strports

    application = service.Application("proxy_modify_request")
    strports.service(portstr, ProxyFactory()).setServiceParent(application)

用法

$ twistd -ny proxy_modify_request.py

在另一个终端:

$ curl -x localhost:8080 http://example.com

这篇关于Python - 扭曲、代理和修改内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆