简体   繁体   中英

Python Twisted proxy - how to intercept packets

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 import reactor
from twisted.python import log
import sys

log.startLogging(sys.stdout)

class ProxyFactory(http.HTTPFactory):
  protocol=proxy.Proxy

reactor.listenTCP(8080, ProxyFactory())
reactor.run()

When I connect my browser to localhost:8080, I can see that all my requests are being directed through the Python proxy running locally. But how do I 1) print out response body and 2) edit the response body before sending it back to the browser?

I hope someone can point me in the right direction - please bear in mind that I'm very new to Python!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM