commit 67b024a0361a0585ebc3bf9ed615ef8bf7c793fd Author: Arturo Filastò arturo@filasto.net Date: Fri Oct 5 11:05:04 2012 +0000
Add utility to obtain IP address of the client based on torproject.org --- ooni/utils/geodata.py | 35 +++++++++++++++++++++++++++++++++++ 1 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/ooni/utils/geodata.py b/ooni/utils/geodata.py new file mode 100644 index 0000000..3e5202e --- /dev/null +++ b/ooni/utils/geodata.py @@ -0,0 +1,35 @@ +import re +from twisted.web.client import Agent +from twisted.internet import reactor, defer, protocol + +class BodyReceiver(protocol.Protocol): + def __init__(self, finished): + self.finished = finished + self.data = "" + + def dataReceived(self, bytes): + self.data += bytes + + def connectionLost(self, reason): + self.finished.callback(self.data) + +@defer.inlineCallbacks +def myIP(): + target_site = 'https://check.torproject.org/' + regexp = "Your IP address appears to be: <b>(.+?)</b>" + + myAgent = Agent(reactor) + result = yield myAgent.request('GET', target_site) + finished = defer.Deferred() + result.deliverBody(BodyReceiver(finished)) + body = yield finished + + match = re.search(regexp, body) + try: + myip = match.group(1) + except: + myip = "unknown" + + return myip + +
tor-commits@lists.torproject.org