MCPcopy
hub / github.com/psf/requests / address_in_network

Function address_in_network

src/requests/utils.py:726–738  ·  view source on GitHub ↗

This function allows you to check if an IP belongs to a network subnet Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24 returns False if ip = 192.168.1.1 and net = 192.168.100.0/24 :rtype: bool

(ip: str, net: str)

Source from the content-addressed store, hash-verified

724
725
726def address_in_network(ip: str, net: str) -> bool:
727 """This function allows you to check if an IP belongs to a network subnet
728
729 Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24
730 returns False if ip = 192.168.1.1 and net = 192.168.100.0/24
731
732 :rtype: bool
733 """
734 ipaddr = struct.unpack("=L", socket.inet_aton(ip))[0]
735 netaddr, bits = net.split("/")
736 netmask = struct.unpack("=L", socket.inet_aton(dotted_netmask(int(bits))))[0]
737 network = struct.unpack("=L", socket.inet_aton(netaddr))[0] & netmask
738 return (ipaddr & netmask) == (network & netmask)
739
740
741def dotted_netmask(mask: int) -> str:

Callers 3

test_validMethod · 0.90
test_invalidMethod · 0.90
should_bypass_proxiesFunction · 0.85

Calls 1

dotted_netmaskFunction · 0.85

Tested by 2

test_validMethod · 0.72
test_invalidMethod · 0.72