[Twisted-Python] matching ip ranges

Maarten ter Huurne maarten at treewalker.org
Wed Aug 29 04:32:10 EDT 2007

On Wednesday 29 August 2007, Alec Matusis wrote:
> We need to match IP ranges of connections, were ranges are specified in the
> "/" notation (e.g.
> What is the best way to do this without consuming too much CPU resources
> when the connection rate is very high?

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).

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20070829/cac96dfd/attachment.pgp 

More information about the Twisted-Python mailing list