Python Twisted proxy - how to intercept packets

后端 未结 1 1400
温柔的废话
温柔的废话 2020-12-28 12:07

I\'m trying to print out the body of a HTTP response using Python.

Here is my code sofar:

from twisted.web import proxy, http
from twisted.internet i         


        
1条回答
  •  無奈伤痛
    2020-12-28 12:38

    Override the dataReceived method of a protocol (proxy.Proxy in your case) and handle the data modification in that method:

    from twisted.web import proxy, http
    from twisted.internet import reactor
    from twisted.python import log
    import sys
    
    log.startLogging(sys.stdout)
    
    class MyProxy(proxy.Proxy):
        def dataReceived(self, data):
    
          # Modify the data here
          print data
    
          # perform the default functionality on modified data 
          return proxy.Proxy.dataReceived(self, data)
    
    class ProxyFactory(http.HTTPFactory):
      protocol=MyProxy
    
    factory = ProxyFactory()
    reactor.listenTCP(8080, factory)
    reactor.run()
    

    0 讨论(0)
提交回复
热议问题