Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To create <code>ProxyFactory</code> that can modify server response headers, content you could override <a href="http://twistedmatrix.com/trac/browser/trunk/twisted/web/proxy.py#L62" rel="noreferrer"><code>ProxyClient.handle*()</code> methods</a>:</p> <pre><code>from twisted.python import log from twisted.web import http, proxy class ProxyClient(proxy.ProxyClient): """Mangle returned header, content here. Use `self.father` methods to modify request directly. """ def handleHeader(self, key, value): # change response header here log.msg("Header: %s: %s" % (key, value)) proxy.ProxyClient.handleHeader(self, key, value) def handleResponsePart(self, buffer): # change response part here log.msg("Content: %s" % (buffer[:50],)) # make all content upper case proxy.ProxyClient.handleResponsePart(self, buffer.upper()) class ProxyClientFactory(proxy.ProxyClientFactory): protocol = ProxyClient class ProxyRequest(proxy.ProxyRequest): protocols = dict(http=ProxyClientFactory) class Proxy(proxy.Proxy): requestFactory = ProxyRequest class ProxyFactory(http.HTTPFactory): protocol = Proxy </code></pre> <p>I've got this solution by looking at the source of <a href="http://twistedmatrix.com/trac/browser/trunk/twisted/web/proxy.py" rel="noreferrer"><code>twisted.web.proxy</code></a>. I don't know how idiomatic it is.</p> <p>To run it as a script or via <code>twistd</code>, add at the end:</p> <pre><code>portstr = "tcp:8080:interface=localhost" # serve on localhost:8080 if __name__ == '__main__': # $ python proxy_modify_request.py import sys from twisted.internet import endpoints, reactor def shutdown(reason, reactor, stopping=[]): """Stop the reactor.""" if stopping: return stopping.append(True) if reason: log.msg(reason.value) reactor.callWhenRunning(reactor.stop) log.startLogging(sys.stdout) endpoint = endpoints.serverFromString(reactor, portstr) d = endpoint.listen(ProxyFactory()) d.addErrback(shutdown, reactor) reactor.run() else: # $ twistd -ny proxy_modify_request.py from twisted.application import service, strports application = service.Application("proxy_modify_request") strports.service(portstr, ProxyFactory()).setServiceParent(application) </code></pre> <h3>Usage</h3> <pre><code>$ twistd -ny proxy_modify_request.py </code></pre> <p>In another terminal:</p> <pre><code>$ curl -x localhost:8080 http://example.com </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload