Skip to content

Instantly share code, notes, and snippets.

@Artanis
Last active December 17, 2015 19:29
Show Gist options
  • Save Artanis/5660864 to your computer and use it in GitHub Desktop.
Save Artanis/5660864 to your computer and use it in GitHub Desktop.
"""Uses tulip to download zeroes.
After getting the zeroes, counts them, and prints the number of zeroes
requested and the the number received.
On my machine, this begins to diverge at approximately 16300 (~16318, and 16384
seems popular). Anything below that is correct, anything above starts losing
bytes. Larger numbers (I accidentally 2**16000 a few times) seem to result in
receiving about 5000.
Update: This is because the response content may be chunked, which means it
arrives in 16k pieces. The answer is to keep reading until you get None.
So while the simplest usage is:
resp = yield from request(...)
data = yield from resp.content.read()
What you may really want is a while loop that exhausts the response.
"""
import tulip
from tulip.http import request
@tulip.coroutine
def get_zeroes(n=10):
### Ask for a resource ###
resp = yield from request("GET", "http://localhost:60000/{0}".format(n))
print("Asked for", n, "bytes")
### Resource is ready ###
### Ask for content (may be chunked) ###
chunk = yield from resp.content.read()
got = 0
body = bytearray()
while chunk: # None if EOS
# length = len(chunk)
# got = got + length
# print("Received", length, "bytes")
### save chunk content ###
body.extend(chunk)
### Ask for next chunk ###
chunk = yield from resp.content.read()
# print("Got", got, "bytes")
# print("Missing", n-got, "bytes")
### Do something with data ###
print("Got", sum(len(c) for c in body), "bytes")
print("-"*80)
loop = tulip.get_event_loop()
for n in range(10, 20):
loop.run_until_complete(get_zeroes(2**n))
for n in range(16000, 17000, 100):
loop.run_until_complete(get_zeroes(n))
"""Serves zeroes.
Don't leave this running, I think, or someone may (try to) make you send them a few gigs worth of zeroes. :)
"""
import bottle
app = bottle.Bottle()
@app.get("/<n:int>")
def index(n):
return "0"*n
bottle.run(app, host="localhost", port=60000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment