7

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!

Eamorr
  • 9,872
  • 34
  • 125
  • 209

1 Answers1

9

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()
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • Hey, many thanks for that reply. I got an exception - File "proxy4.py", line 15, in dataReceived return super(MyProxy,self).dataReceived(data) exceptions.TypeError: must be type, not classobj – Eamorr Jan 30 '12 at 15:32
  • Corrected the code. Can you check if it is still raising TypeError?. – Mariusz Jamro Feb 02 '12 at 12:39
  • 1
    you might mean: `proxy.Proxy.dataReceived(self, data)` instead of `MyProxy.dataReceived(self, data)` othewise it leads to stackoverflow. – jfs Feb 27 '12 at 18:16
  • 1
    Also `dataReceived()` sees only data send by client to server. Getting the data send by server to client might be [more complicated](http://stackoverflow.com/a/9469400/4279) – jfs Feb 27 '12 at 18:19
  • @J.F.Sebastian Tried the code using Chrome and Safari and received an `Unhandled Error`. Are you getting the same problem? – Nyxynyx Jul 16 '14 at 19:49
  • @Nyxynyx: No. Are you trying to visit the proxy as a web server? You should not put the proxy url into the browser address bar. Configure network settings to use the proxy as a proxy instead. Note: only simple html pages will work if their content is uppercased (`buffer.upper()`) – jfs Jun 25 '15 at 15:44