繁体   English   中英

扭曲的Python代理

[英]Python Proxy with Twisted

你好! 我有这个代码:

from twisted.web import proxy, http
from twisted.internet import reactor

class akaProxy(proxy.Proxy):
    """
    Local proxy = bridge between browser and web application
    """

    def dataReceived(self, data):

        print "Received data..."

        headers = data.split("\n")
        request = headers[0].split(" ")

        method = request[0].lower()
        action = request[1]
        print action
        print "ended content manipulation"  
        return proxy.Proxy.dataReceived(self, data)

class ProxyFactory(http.HTTPFactory):
    protocol = akaProxy

def intercept(port):
    print "Intercept"
    try:                
        factory = ProxyFactory()
        reactor.listenTCP(port, factory)
        reactor.run()
    except Exception as excp:
        print str(excp)

intercept(1337)

我使用上面的代码来拦截浏览器和网站之间的所有内容。 使用上述内容时,我配置了我的浏览器设置:IP:127.0.0.1和端口:1337。我将此脚本放在远程服务器中,以将我的远程服务器作为代理服务器。 但是,当我将浏览器代理IP设置更改为我的服务器时,它不起作用。 我做错了什么? 还需要配置什么?

据推测,您的dataReceived在尝试解析传递给它的数据时会引发异常。 尝试启用日志记录,以便您可以查看更多正在进行的操作:

from twisted.python.log import startLogging
from sys import stdout
startLogging(stdout)

您的解析器可能引发异常的原因是dataReceived不会仅使用完整请求进行调用。 使用从TCP连接读取的任何字节调用它。 这可能是完整请求,部分请求,甚至是两个请求(如果正在使用流水线操作)。

Proxy上下文中的dataReceived正在处理“将rawData转换为行”,因此尝试操作代码可能为时尚早。 您可以尝试覆盖allContentReceived ,您将可以访问完整的标题和内容。 这是一个我相信你做的事情的例子:

#!/usr/bin/env python
from twisted.web import proxy, http

class SnifferProxy(proxy.Proxy):
    """
    Local proxy = bridge between browser and web application
    """

    def allContentReceived(self):
        print "Received data..."
        print "method = %s" % self._command
        print "action = %s" % self._path
        print "ended content manipulation\n\n"
        return proxy.Proxy.allContentReceived(self)


class ProxyFactory(http.HTTPFactory):

    protocol = SnifferProxy

if __name__ == "__main__":
    from twisted.internet import reactor
    reactor.listenTCP(8080, ProxyFactory())
    reactor.run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM