Edit: How should I interpret the silence? On a scale from 0 to 10 where 0 means "Bloody awful" and 10 means "Nothing to complain about".
I'm mainly concerned about readability and things I don't seem to be aware of. If the code is readable, I won't need to explain it.
#!/usr/bin/python
import os
import sys
import subprocess
assert __name__ != '__main__'
max_load_avg1 = 3.5
'''
compressout
===========
Simple CGI output module.
Uses gzip compression on the output stream if the client accepts it.
NOTICE: The `cgitb` module will write to stdout if the script crashes,
you should use a browser that does not accept gzip, when you are
testing your scripts.
NOTICE: In the beginning of this file `max_load_avg1` is defined.
This is the maximum allowed load average under one minute.
If the one minute load average exceeds this value, compressout will
abort.
Functions
=========
init(write_headers=True)
------------------------
Initialize the module. This function will detect if the client
supports gzip.
If `write_headers`, write a 'Vary' and (if used)
'Content-Encdoing' header.
write_h(s)
----------
Write part of header.
Write `s` to standard output, will never go through gzip.
write_b(s)
----------
Write part of body.
gzip is supported by the client
-------------------------------
`s` will be appended to a local buffer
which `done` will compress and print.
gzip is not supported
---------------------
`s` will go straight to stdout.
done()
------
Done writing output.
This function will invoke gzip.
Dos and don'ts
==============
## ## ##
if __name__ == '__main__':
compressout.init()
main()
compressout.done()
## ## ##
* Never call `write_h` after any call to `write_b`
* Always call `done` when your done.
* Use only compressout to write output
* NOTICE: The `cgitb` module will write to stdout if the script
crashes, you should use a browser that does not accept gzip,
when you are testing your scripts.
'''
http503_body = '''
Service temporarily unavailable!
Wait at least two minutes before trying again.
Re-attempting prematurely may result in banning your IP address.
-- END OF TRANSMISSION --
'''
def init(write_headers=True):
'''
Initialize the module. This function will detect if the client
support gzip.
If `write_headers`, write a 'Vary' and (if used)
'Content-Encoding' header.
'''
global use_gzip
global body
# This is the only place where sending a 503 message will work.
# write_h:
# - Message body may need to be compressed.
# - Possibility of conflicting Status headers.
# write_b:
# - Message body may need to be compressed.
# - Message body may be application/xhtml+xml
# done:
# - Message body needs to be compressed if `use_gzip`.
# - Body has already been written if not `use_gzip`.
if os.getloadavg()[0] > max_load_avg1:
sys.stdout.write('Status: 503\n')
sys.stdout.write('Content-Type: text/plain\n')
sys.stdout.write('Retry-After: 90\n')
sys.stdout.write(http503_body)
sys.stdout.flush()
os.abort()
use_gzip = 'gzip' in os.getenv('HTTP_ACCEPT_ENCODING', '')
body = ''
if write_headers:
sys.stdout.write('Vary: Accept-Encoding\n')
if use_gzip:
sys.stdout.write('Content-Encoding: gzip\n')
def write_h(s):
'''
Write part of header.
Write `s` to standard output, will never go through gzip.
'''
if os.getloadavg()[0] > max_load_avg1: os.abort()
sys.stdout.write(s)
def write_b(s):
'''
Write part of body.
gzip is supported by the client
-------------------------------
`s` will be appended to a local buffer
which `done` will compress and print.
gzip is not supported
---------------------
`s` will go straight to stdout.
'''
global body
if os.getloadavg()[0] > max_load_avg1: os.abort()
if use_gzip:
body += s
else:
sys.stdout.write(s)
def done():
'''
Done writing output.
This function will invoke gzip.
'''
if os.getloadavg()[0] > max_load_avg1: os.abort()
if use_gzip:
gzip = subprocess.Popen(
['gzip'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
sys.stdout.write(gzip.communicate(body)[0])
use_gzip = True or False
body = ''
I realize there are a few things I should have commented when I edited this module a while ago. But for "correctness" sake, I won't change the code in the question.
The current code can be found at: https://oskog97.com/read/?path=/compressout.py