1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 An example of a proxy which logs all requests processed through it.
8 $ python logging-proxy.py
10 Then configure your web browser to use localhost:8080 as a proxy, and visit a
11 URL (This is not a SOCKS proxy). When browsing in this configuration, this
12 example will proxy connections from the browser to the server indicated by URLs
13 which are visited. The client IP and the request hostname will be logged for
16 HTTP is supported. HTTPS is not supported.
18 See also proxy.py for a simpler proxy example.
21 from twisted.internet import reactor
22 from twisted.web import proxy, http
24 class LoggingProxyRequest(proxy.ProxyRequest):
27 It's normal to see a blank HTTPS page. As the proxy only works
28 with the HTTP protocol.
30 print "Request from %s for %s" % (
31 self.getClientIP(), self.getAllHeaders()['host'])
33 proxy.ProxyRequest.process(self)
35 print "HTTPS is not supported at the moment!"
37 class LoggingProxy(proxy.Proxy):
38 requestFactory = LoggingProxyRequest
40 class LoggingProxyFactory(http.HTTPFactory):
41 def buildProtocol(self, addr):
44 reactor.listenTCP(8080, LoggingProxyFactory())