<s> """<STR_LIT>""" <EOL> import time <EOL> ip_hash = { } <EOL> subject_hash = { } <EOL> aging_hash_interval = <NUM_LIT> <EOL> ip_hash_threshold = <NUM_LIT:20> <EOL> subject_hash_threshold = <NUM_LIT> <EOL> def dump_hash ( aging_hash ) : <EOL> """<STR_LIT>""" <EOL> for k , v in aging_hash . items ( ) : <EOL> print "<STR_LIT>" % ( k , v ) <EOL> def purge_expired ( aging_hash , interval = aging_hash_interval ) : <EOL> """<STR_LIT>""" <EOL> expired = [ ] <EOL> for k , v in aging_hash . items ( ) : <EOL> set_time = v [ <NUM_LIT:0> ] <EOL> if ( time . time ( ) - set_time ) > aging_hash_interval : <EOL> expired . append ( k ) <EOL> for ex_k in expired : <EOL> del aging_hash [ ex_k ] <EOL> def update_aging_hash ( aging_hash , k , increment = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not aging_hash . has_key ( k ) : <EOL> aging_hash [ k ] = ( time . time ( ) , increment ) <EOL> else : <EOL> current_val = aging_hash [ k ] <EOL> aging_hash [ k ] = ( current_val [ <NUM_LIT:0> ] , increment + current_val [ <NUM_LIT:1> ] ) <EOL> return aging_hash [ k ] <EOL> def valid_key ( k , aging_hash , frequency_threshold ) : <EOL> """<STR_LIT>""" <EOL> purge_expired ( aging_hash ) <EOL> current_val = update_aging_hash ( aging_hash , k ) <EOL> return current_val [ <NUM_LIT:1> ] <= frequency_threshold <EOL> def valid_ip_address ( ip_address ) : <EOL> """<STR_LIT>""" <EOL> return valid_key ( ip_address , ip_hash , ip_hash_threshold ) <EOL> def valid_subject ( subject ) : <EOL> """<STR_LIT>""" <EOL> return valid_key ( subject , subject_hash , subject_hash_threshold ) <EOL> def block_ip_address ( ip_address ) : <EOL> """<STR_LIT>""" <EOL> update_aging_hash ( ip_hash , ip_address , ip_hash_threshold ) <EOL> def block_subject ( subject ) : <EOL> """<STR_LIT>""" <EOL> update_aging_hash ( subject_hash , subject , subject_hash_threshold ) </s>
