Created
July 6, 2017 21:42
-
-
Save thorsummoner/a07eac1411a2d1d9dbf125ec10366206 to your computer and use it in GitHub Desktop.
Serve file of size N over facecgi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment