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)

上記のコードを使用して、ブラウザとWebサイトの間のすべてを傍受します。上記を使用する場合、ブラウザの設定をIP:127.0.0.1およびポート:1337に構成します。このスクリプトをリモートサーバーに配置して、リモートサーバーをプロキシサーバーとして機能させます。しかし、ブラウザのプロキシIP設定をサーバーに変更すると、機能しません。私は何を間違えますか?他に何を設定する必要がありますか?

4

2 に答える 2

2

おそらく、dataReceived渡されたデータを解析しようとしているときに例外が発生しています。ログを有効にして、何が起こっているかをもっと確認できるようにしてください。

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

パーサーが例外を発生させる可能性が高い理由はdataReceived、完全なリクエストでのみ呼び出されないためです。これは、TCP 接続から読み取られたバイトで呼び出されます。これは、完全な要求、部分的な要求、または 2 つの要求 (パイプラインが使用されている場合) の場合があります。

于 2012-08-03T18:31:36.413 に答える
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()
于 2012-08-30T13:11:24.770 に答える