[Twisted-Python] matching ip ranges
![](https://secure.gravatar.com/avatar/45c4c3d016586cd3f4f3adcc3f0c104d.jpg?s=120&d=mm&r=g)
We need to match IP ranges of connections, were ranges are specified in the "/" notation (e.g. 10.10.2.0/24) What is the best way to do this without consuming too much CPU resources when the connection rate is very high?
![](https://secure.gravatar.com/avatar/fbd473e7e3b6675a84cd3c3b4a2c1972.jpg?s=120&d=mm&r=g)
On Wednesday 29 August 2007, Alec Matusis wrote:
Convert the IP address to an integer and check using an AND mask: if ip & mask == base: # in range where "ip" is the IP address of the incoming connection as an integer, "mask" is the netmask as an integer (/24 => 0xFFFFFF00) and "base" is the lowest IP address in the range ("base & mask == base" must hold). Bye, Maarten
![](https://secure.gravatar.com/avatar/7905d47e9096477130b7055ab70d0f30.jpg?s=120&d=mm&r=g)
Alec Matusis wrote:
Take a look at the AddressRestrictorMixin in twisted_goodies.misc: http://foss.eepatents.com/trac/Twisted-Goodies/browser/projects/Twisted-Good... Best regards, Ed
![](https://secure.gravatar.com/avatar/fbd473e7e3b6675a84cd3c3b4a2c1972.jpg?s=120&d=mm&r=g)
On Wednesday 29 August 2007, Alec Matusis wrote:
Convert the IP address to an integer and check using an AND mask: if ip & mask == base: # in range where "ip" is the IP address of the incoming connection as an integer, "mask" is the netmask as an integer (/24 => 0xFFFFFF00) and "base" is the lowest IP address in the range ("base & mask == base" must hold). Bye, Maarten
![](https://secure.gravatar.com/avatar/7905d47e9096477130b7055ab70d0f30.jpg?s=120&d=mm&r=g)
Alec Matusis wrote:
Take a look at the AddressRestrictorMixin in twisted_goodies.misc: http://foss.eepatents.com/trac/Twisted-Goodies/browser/projects/Twisted-Good... Best regards, Ed
participants (3)
-
Alec Matusis
-
Ed Suominen
-
Maarten ter Huurne