Skip to content

Instantly share code, notes, and snippets.

@puentesarrin
Created June 29, 2012 21:30
Show Gist options
  • Save puentesarrin/3020783 to your computer and use it in GitHub Desktop.
Save puentesarrin/3020783 to your computer and use it in GitHub Desktop.
Logging Twisted WebProxy requests with MongoDB
from twisted.internet import reactor
from twisted.web import proxy, http
import datetime
import pymongo
db = pymongo.Connection()['proxy']
class LoggingProxyRequest(proxy.ProxyRequest):
def process(self):
db.log.insert({"method": self.method,
"path": self.path,
"clientip": str(self.getClientIP()),
"headers": self.getAllHeaders(),
"args": self.args,
"date": datetime.datetime.now()})
try:
proxy.ProxyRequest.process(self)
except KeyError:
print "HTTPS is not supported at the moment!"
class LoggingProxy(proxy.Proxy):
requestFactory = LoggingProxyRequest
class LoggingProxyFactory(http.HTTPFactory):
def buildProtocol(self, addr):
return LoggingProxy()
reactor.listenTCP(8080, LoggingProxyFactory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment