| OLD | NEW |
| (Empty) | |
| 1 def main(request, response): |
| 2 """Code for generating large responses where the actual response data |
| 3 isn't very important. |
| 4 |
| 5 Two request parameters: |
| 6 size (required): An integer number of bytes (no suffix) or kilobytes |
| 7 ("kb" suffix) or megabytes ("Mb" suffix). |
| 8 string (optional): The string to repeat in the response. Defaults to "a". |
| 9 |
| 10 Example: |
| 11 /resources/large.py?size=32Mb&string=ab |
| 12 """ |
| 13 if not "size" in request.GET: |
| 14 400, "Need an integer bytes parameter" |
| 15 |
| 16 bytes_value = request.GET.first("size") |
| 17 |
| 18 chunk_size = 1024 |
| 19 |
| 20 multipliers = {"kb": 1024, |
| 21 "Mb": 1024*1024} |
| 22 |
| 23 suffix = bytes_value[-2:] |
| 24 if suffix in multipliers: |
| 25 multiplier = multipliers[suffix] |
| 26 bytes_value = bytes_value[:-2] * multiplier |
| 27 |
| 28 try: |
| 29 num_bytes = int(bytes_value) |
| 30 except ValueError: |
| 31 return 400, "Bytes must be an integer possibly with a kb or Mb suffix" |
| 32 |
| 33 string = str(request.GET.first("string", "a")) |
| 34 |
| 35 chunk = string * chunk_size |
| 36 |
| 37 def content(): |
| 38 bytes_sent = 0 |
| 39 while bytes_sent < num_bytes: |
| 40 if num_bytes - bytes_sent < len(chunk): |
| 41 yield chunk[num_bytes - bytes_sent] |
| 42 else: |
| 43 yield chunk |
| 44 bytes_sent += len(chunk) |
| 45 return [("Content-Type", "text/plain")], content() |
| OLD | NEW |