[英]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.