2012-08-03 27 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