phihag / ipaddress

Python 3.3+'s ipaddress for older Python versions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ip_network iteration issues -- am I doing this right?

vampjaz opened this issue · comments

commented

I'm trying to write a script to iterate over all the addresses in a given range specified. Taking from the official docs, I wrote a script like this:

import ipaddress
net4 = ipaddress.ip_network('192.0.2.0/24')
for x in net4.hosts():
    print(x) 

However, instead of printing the 255 addresses, it gave me an error:

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    net4 = ipaddress.ip_network('192.0.2.0/24')
  File "build/bdist.macosx-10.10-x86_64/egg/ipaddress.py", line 196, in ip_network
ValueError: '192.0.2.0/24' does not appear to be an IPv4 or IPv6 network

However, if I change the ip mask to 192.168.225.0/24, it runs without error, but prints nothing.

I'm wondering, am I doing this wrong? Shouldn't I get a list??

Thank you for the report! By default, in Python 2.x, string literals produce byte strings. For compatibility with Python 3.x and sanity, you want character strings though. There's a simple fix: Just stick
from __future__ import unicode_literals at the start of your script or console session. See the README for more information.

In ipaddress 1.0.15 and newer, I have modified the warning to include more details.