1

I am trying to log requests and responses and the headers of both using a Twisted Framework's Proxy.

Right now the requests are handled by process method in HTTPProxyRequest class and the responses are handled in HTTPProxyClient.

I want to know know which response corresponds to which request. What is the least hackish way of doing this?

from twisted.python import log
from twisted.web import http
from twisted.web.proxy import Proxy, ProxyRequest, ProxyClientFactory, ProxyClient



class HTTPProxyClient(ProxyClient):
    def __init__(self, command, rest, version, headers, data, father):
        ProxyClient.__init__(self, command, rest, version, headers, data, father)
        self.new_buffer = ""
        self.new_headers = {}
    def handleHeader(self, key, value):
        self.new_headers[key] = value
        ProxyClient.handleHeader(self, key, value)

    def handleResponsePart(self, buffer):
        # log.msg("RESPONSE_CONTENT: %s" % buffer)
        ProxyClient.handleResponsePart(self, buffer)
        self.new_buffer += buffer

    def handleResponseEnd(self):
        log.msg("RESPONSE_HEADERS:%s \n RESPONSE_CONTENT: %s" % (str(self.new_headers),self.new_buffer))
        ProxyClient.handleResponseEnd(self)

class HTTPProxyFactory(ProxyClientFactory):
    protocol = HTTPProxyClient

class HTTPProxyRequest(ProxyRequest):
    protocols = {'http' : HTTPProxyFactory}

    def process(self):
        log.msg("REQUEST_METHOD:", self.method)
        # log.msg(self.content.read())
        for k,v in self.requestHeaders.getAllRawHeaders():
            # log.msg("REQUEST_HEADER: %s : %s" % (k,v))
            pass
        # log.msg("\n \n")
        if self.method == "POST":
            log.msg("LOGGING POST CONTENT:", self.content.read())

        ProxyRequest.process(self)

class HTTPProxy(Proxy):

    requestFactory = HTTPProxyRequest


if __name__ == '__main__': # $ python proxy_modify_request.py
    import sys
    from twisted.internet import reactor
    log.startLogging(sys.stdout)
    factory = http.HTTPFactory()
    factory.protocol = HTTPProxy

    reactor.listenTCP(8070, factory)
    reactor.run()
4

0 回答 0