Created
June 29, 2012 21:30
-
-
Save puentesarrin/3020783 to your computer and use it in GitHub Desktop.
Logging Twisted WebProxy requests with MongoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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