Skip to content

Instantly share code, notes, and snippets.

@thorsummoner
Created March 16, 2017 18:35
Show Gist options
  • Save thorsummoner/e5eeb883f62489fa6adc728c1b109aeb to your computer and use it in GitHub Desktop.
Save thorsummoner/e5eeb883f62489fa6adc728c1b109aeb to your computer and use it in GitHub Desktop.
Dispatch zero-fill file of user request size over the network.
#!/usr/bin/python3
import os
import subprocess
import re
import humanfriendly
SIZE_RE = re.compile("^\d{,4}([KMG])$")
def main():
file_size = (
os.environ.get('QUERY_STRING')
or os.environ.get('REQUEST_URI').split('/nettest/', 1)[-1]
or '100M'
).upper().rstrip('B')
if not SIZE_RE.match(file_size):
print('Content-Type: text/html', end='\n\n')
print('Couldn\'t parse dd size "{}" (four digits max)'.format(file_size))
raise SystemExit('Bad DD Size')
file_bytes = humanfriendly.parse_size(file_size+'B', binary=True)
_cmd = ['dd', 'if=/dev/zero', 'of=/dev/stdout', 'count=1', 'bs={}'.format(file_size)]
print('Content-Type: application/octet-stream')
print('Content-Type: application/octet-stream')
print('Content-Disposition: attachment; filename="nettest-{}B.bin"'.format(file_size))
print('Content-length: {}'.format(file_bytes), end='\n\n')
popen = subprocess.Popen(
_cmd,
stderr=subprocess.PIPE,
)
if __name__ == '__main__':
main()
server {
location /nettest/ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_params SCRIPT_FILENAME /srv/cgi-bin/nettest.py;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment