From 07b0d1dce24de91639e1317bf9b463d10d39b0c7 Mon Sep 17 00:00:00 2001 From: grun Date: Fri, 28 Feb 2014 01:48:27 -0800 Subject: [PATCH] Add /drip, which drips data over a duration after an optional initial delay. --- README.md | 1 + httpbin/core.py | 20 ++++++++++++++++++++ httpbin/templates/httpbin.1.html | 1 + 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 4d5e31c..0defe8d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Freely hosted in [HTTP](http://httpbin.org) & - [`/digest-auth/:qop/:user/:passwd`](http://httpbin.org/digest-auth/auth/user/passwd) Challenges HTTP Digest Auth. - [`/stream/:n`](http://httpbin.org/stream/20) Streams *n*–100 lines. - [`/delay/:n`](http://httpbin.org/delay/3) Delays responding for *n*–10 seconds. +- [`/drip?numbytes=n&duration=s&delay=s`](http://httpbin.org/drip?numbytes=5&duration=5) Drips data over a duration after an optional initial delay. - [`/html`](http://httpbin.org/html) Renders an HTML Page. - [`/robots.txt`](http://httpbin.org/robots.txt) Returns some robots.txt rules. - [`/deny`](http://httpbin.org/deny) Denied by robots.txt file. diff --git a/httpbin/core.py b/httpbin/core.py index 3fdf8c9..e323c11 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -374,6 +374,26 @@ def delay_response(delay): return jsonify(get_dict( 'url', 'args', 'form', 'data', 'origin', 'headers', 'files')) +@app.route('/drip') +def drip(): + """Drips data over a duration after an optional initial delay.""" + args = CaseInsensitiveDict(request.args.items()) + duration = float(args.get('duration', 2)) + numbytes = int(args.get('numbytes', 10)) + pause = duration / numbytes + + delay = float(args.get('delay', 0)) + if delay > 0: + time.sleep(delay) + + def generate_bytes(): + for i in xrange(numbytes): + yield bytes(chr(42)) + time.sleep(pause) + + return Response(generate_bytes(), headers={ + "Content-Type": "application/octet-stream", + }) @app.route('/base64/') def decode_base64(value): diff --git a/httpbin/templates/httpbin.1.html b/httpbin/templates/httpbin.1.html index 2a9d1f5..7c4a786 100644 --- a/httpbin/templates/httpbin.1.html +++ b/httpbin/templates/httpbin.1.html @@ -30,6 +30,7 @@
  • /digest-auth/:qop/:user/:passwd Challenges HTTP Digest Auth.
  • /stream/:n Streams n–100 lines.
  • /delay/:n Delays responding for n–10 seconds.
  • +
  • /drip?numbytes=n&duration=s&delay=s Drips data over a duration after an optional initial delay.
  • /html Renders an HTML Page.
  • /robots.txt Returns some robots.txt rules.
  • /deny Denied by robots.txt file.