Last active
December 15, 2015 18:09
-
-
Save jonaslindmark/5301814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 time import time | |
from twisted.web import client, error as web_error | |
from twisted.python import failure | |
from cStringIO import StringIO | |
import gzip | |
port = 8098 | |
def uncompress(compressed_data): | |
sio = StringIO(compressed_data) | |
with gzip.GzipFile(fileobj=sio, mode='rb') as zf: | |
data = zf.read() | |
return data | |
def get(host, bucket, key, timeout=0.5): | |
start = time() | |
def handle_result(result): | |
end = time() | |
print "Riak result in: %s ms", (end-start) | |
return uncompress(result) | |
def build_url(host): | |
return str(url) | |
def on_404(failure): | |
if failure.check(web_error.Error) and failure.value.status == 404: | |
return None | |
return failure | |
url = 'http://%s:%s/riak/%s/%s' % (host, port, bucket, key) | |
d = client.getPage(url , timeout=timeout) | |
d.addCallbacks(handle_result, on_404) | |
return d | |
if __name__ == "__main__": | |
from twisted.internet import reactor | |
host = 'riak-02.production.wrops.net' | |
def got_result(r): | |
print "got result", r[:20] | |
reactor.stop() | |
d = get(host, 'friendships','7587') | |
d.addCallback(got_result) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build_url doesn't seem to be used