Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dispatch zero-fill file of user request size over the network.
import os
import subprocess
import re
import humanfriendly
SIZE_RE = re.compile("^\d{,4}([KMG])$")
def main():
file_size = (
or os.environ.get('REQUEST_URI').split('/nettest/', 1)[-1]
or '100M'
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(
if __name__ == '__main__':
server {
location /nettest/ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_params SCRIPT_FILENAME /srv/cgi-bin/;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment