2012-08-03 35 views
3

您好!我有这样的代码:双绞线代理服务器

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设置更改为我的服务器时,它不起作用。我做错了什么?还有什么我需要配置?

回答

2

假设您的dataReceived在尝试解析传递给它的数据时引发异常。尝试启用日志记录,所以你可以看到更多的正在发生的事情的:

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

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

0

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() 
+1

我抄,放在我的远程服务器上的脚本并运行它,而与服务器的连接保持打开状态。然后,我将浏览器的代理ip改为访问我的服务器以访问http://www.ifconfig.me/ip,但我无法浏览器显示“连接超时”,服务器上的任何打开的python脚本都不显示任何信息。它保持原样。 – torayeff 2012-08-30 18:16:43

+1

除了将您的浏览器代理设置更改为您的服务器的IP,并且还需要将代理端口设置为8080. – Braudel 2012-08-30 23:27:12

+1

您认为我没有:) – torayeff 2012-08-30 23:32:35