Traceback (most recent call last): File…
- User
Traceback (most recent call last): File "C:/Users/cunbe/AppData/Local/Programs/Python/Python311/111.py", line 7, in if f'{t1:b}'.count('1')>co: TypeError: unsupported format string passed to IPv4Network.format
from ipaddress import * co = 0 for x in range (33): t1 = ip_network('165.112.200.70/'+str(x),0) t2 = ip_network('165.112.175.80/'+str(x),0) if t1 == t2: if f'{t1:b}'.count('1')>co: co = f'{t1:b}'.count('1') print(co)
- Assistant
From the error message provided, it seems like you are trying to use a format string to convert the
IPv4Networkobjectt1into a binary representation, which is not directly supported. In Python'sipaddressmodule,IPv4Networkobjects do not support the:bformat specifier directly as you might use with integers.Your code tries to check if two IP networks are equal, and then count the number of '1's in the binary representation of the network if they are. However, to get a binary representation from an
IPv4Networkobject, you will have to work with the network's address components (e.g., the network address or the individual addresses within the network) since the object itself cannot be directly formatted to binary as you attempted.Also, your intention of comparing IP networks and then counting '1's in their binary representation seems to be a bit unclear because you're directly comparing the networks with
==. If your aim is to look at the similarity in terms of subnet parts or something else, you'll need a different approach.Let's clarify the purpose and give you a revised version based on a guess of what you might be trying to do. If your goal is to compare whether the network parts of two IPs are the same for different subnet masks and then find the binary representation of that subnet mask (or network) with the most '1's, you'd typically compare the network addresses directly. However, you need a different method to achieve a meaningful comparison and count binaries.
Below is an adjustment of your code. This revised version doesn't aim to match your original intent perfectly because that intent isn't fully clear, but it should give you a start on how to handle the objects correctly:
pythonfrom ipaddress import ip_network co = 0 for x in range (33): t1 = ip_network('165.112.200.70/'+str(x), False) t2 = ip_network('165.112.175.80/'+str(x), False) if t1.overlaps(t2): # Get the netmask as an integer for counting '1's in its binary representation. netmask_int = int(t1.netmask) count_ones = bin(netmask_int).count('1') if count_ones > co: co = count_ones print(co)Here, we are using
t1.netmaskto retrieve the netmask of the networkt1as an IPv4Address object, converting it to an integer withint(), and then to a binary string withbin()to count '1's. Note thatoverlaps(t2)is used instead of==since direct comparison might not be what you're looking for if you want to compare network overlap. However, this code may still not fully align with your intended logic, especially since it's unclear what the specific comparison betweent1andt2is meant to achieve.