Debian Codegraph @

docs/web/examples/logging-proxy.py

raw
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 An example of a proxy which logs all requests processed through it.
6
7 Usage:
8 $ python logging-proxy.py
9
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
14 each request.
15
16 HTTP is supported. HTTPS is not supported.
17
18 See also proxy.py for a simpler proxy example.
19 """
20
21 from twisted.internet import reactor
22 from twisted.web import http, proxy
23
24
25 class LoggingProxyRequest(proxy.ProxyRequest):
26 def process(self):
27 """
28 It's normal to see a blank HTTPS page. As the proxy only works
29 with the HTTP protocol.
30 """
31 print(
32 "Request from %s for %s"
33 % (self.getClientIP(), self.getAllHeaders()["host"])
34 )
35 try:
36 proxy.ProxyRequest.process(self)
37 except KeyError:
38 print("HTTPS is not supported at the moment!")
39
40
41 class LoggingProxy(proxy.Proxy):
42 requestFactory = LoggingProxyRequest
43
44
45 class LoggingProxyFactory(http.HTTPFactory):
46 def buildProtocol(self, addr):
47 return LoggingProxy()
48
49
50 reactor.listenTCP(8080, LoggingProxyFactory())
51 reactor.run()