def nasty_before_handler(nasty_depot, maxroll=100):
"""Cherrypy Tool callable which generates various problems prior to a
request. Possible outcomes: retryable HTTP error, short nap."""
# Must be set in _cp_config on associated request handler.
assert nasty_depot
# Adjust nastiness values once per incoming request.
nasty_depot.nasty_housekeeping()
# Just roll the main nasty dice once.
if not nasty_depot.need_nasty(maxroll=maxroll):
return False
while True:
roll = random.randint(0, 10)
if roll == 0:
nasty_depot.nasty_nap()
if random.randint(0, 1) == 1:
# Nap was enough. Let the normal handler run.
return False
if 1 <= roll <= 8:
nasty_depot.nasty_raise_error()
else:
cherrypy.log("NASTY: return bogus or empty response")
response = cherrypy.response
response.body = random.choice(['',
'set this is a bogus action',
'Instead of office chair, '
'package contained bobcat.',
'{"this is a": "fragment of json"}'])
return True
return False
评论列表
文章目录