| Conditions | 2 |
| Total Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | from datetime import datetime, timedelta |
||
| 16 | def attempt(self, ip): |
||
| 17 | """Record a connection attempt |
||
| 18 | |||
| 19 | Returns True if number of recorded attempts smaller than allowed number, |
||
| 20 | False otherwise |
||
| 21 | """ |
||
| 22 | # Check for time window rollover |
||
| 23 | now = datetime.now() |
||
| 24 | if now - self.ts > CONN_TIME: |
||
| 25 | # Reset everything if we're rolled over |
||
| 26 | self.conn_attempts = {} |
||
| 27 | self.ts = now |
||
| 28 | |||
| 29 | self.conn_attempts[ip] = self.conn_attempts.get(ip, 0) + 1 |
||
| 30 | |||
| 31 | return self.conn_attempts[ip] <= CONN_ATTEMPTS |
||
| 32 |