Giter Site home page Giter Site logo

ipaddress's Introduction

ipaddress

Python 3.3+'s ipaddress for Python 2.6, 2.7, 3.2.

This repository tracks the latest version from cpython, e.g. ipaddress from cpython 3.8 as of writing.

Note that just like in Python 3.3+ you must use character strings and not byte strings for textual IP address representations:

>>> from __future__ import unicode_literals
>>> ipaddress.ip_address('1.2.3.4')
IPv4Address(u'1.2.3.4')

or

>>> ipaddress.ip_address(u'1.2.3.4')
IPv4Address(u'1.2.3.4')

but not:

>>> ipaddress.ip_address(b'1.2.3.4')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '1.2.3.4' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?

ipaddress's People

Contributors

chrahunt avatar hroncok avatar jdufresne avatar mindw avatar phihag avatar tkem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ipaddress's Issues

127.0.0.1 is not considered as private

$ python2
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.ip_address(u'127.0.0.1').is_private
False

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.ip_address(u'127.0.0.1').is_private
True

ip_network iteration issues -- am I doing this right?

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??

Support Python 3.3+ with importing through another package name

Hi there!

I like this backport a lot - it comes in handy often. It is assumed it's a backport of 3.3+ ipaddress to older Python versions, but in fact it's a backport of a much fresher version, which is even cooler.

The point is, when you install that package, it yields ipaddress.py in your sitepackages, so there is no non-hackish way of importing this version of ipaddress over a system module in Python 3.3+.

I'm working with API parts fully supported in Python 3.7 and this backport, but not available in Python 3.3s version of the module (IPv4Constants, subnet_of, supernet_of introduced in 3.7). It works perfectly fine with Python 3.7 with native module and in Python 2.7 when the backport is imported, but import ipaddress` in Python 3.3+,<3.7 uses the system module over the backport, and does not expose the fresher API.

It would be nice if the package was also available under a different, non-shadowing name, so it can be explicitly imported to be used with any Python version.

About to get dropped from gentoo portage because of python 2.7

Today when running OS updates on gentoo hosts I was greeted with a warning that ipaddress will be dropped from the portage tree in 30 days.

https://packages.gentoo.org/packages/dev-python/ipaddress
https://bugs.gentoo.org/743724

I think this is because you're not keeping up with the ever-moving target of python versions. 2.7 and 3.6 are currently bad. Of these, 2.7 is a death sentence.

It would be nice if this important library didn't get pulled because of a lack of housekeeping. Can I help? I have a vested interest in keeping this package in gentoo. I'm not affiliated with the gentoo project. I'm just a programmer who needs this library for my job.

RPM packaging?

Hi.

I've this backport-library as dependency in my project with python 2.6 and 2.7 support. I want to package my project in RPM to ease shipping for RHEL/CentOS 6/7 users. So, I need to duplicate dependencies in .spec. So, I need RPM version of each dependency.

I don't want to bother you a lot with this "feature", but:

  • would you like to accept a pull request if I'll will do all the packaging routine?
  • Or maybe you already planned to do it?
  • And I also don't know who should be a maintainer of packaged PRM (I want to submit it to epel or somewhere else).

What do you think? (Sorry for poor english).

ip_network and '::/0'

Using Python 2.7.5, ipaddress-1.0.18-py2-none-any.whl
I'm encountering the following peculiarity when using ip_network(data).version to determine whether a netmask is IPv4 or IPv6:

Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress;
>>> ipaddress.ip_network('::/0').version
4
>>> ipaddress.ip_network(u'::/0').version
6

Is this by design, or can this be fixed?

Compatibility for Python 3.11x

Hello,
According to the PyPi Doc the latest python version supported to ipaddress v1.0.23 is python 3.6.
Does it also supports python 3.11x?
if no, is there any plan to support it?

Bug with str (not unicode) ip address

In [1]: ipaddress.ip_address(u'192.0.2.1')
Out [1]: IPv4Address(u'192.0.2.1')

In [2]: ipaddress.ip_address('192.0.2.1')

ValueError Traceback (most recent call last)
in ()
----> 1 ipaddress.ip_address('192.0.2.1')

/home/env/local/lib/python2.7/site-packages/ipaddress-1.0.3-py2.7.egg/ipaddress.pyc in ip_address(address)
103
104 raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
--> 105 address)
106
107

ValueError: '192.0.2.1' does not appear to be an IPv4 or IPv6 address

class IPv4Network hosts function does not work when prefixlen is /31

Normally, the class IPv4Network "hosts" function is modified to "iter" when mask is /31.
This works well when the network is a string, however when the network is an integer, it does not work.

In the init function of the class, there are two conditions upon which the hosts function is not adjusted:

if isinstance(address, (_compat_int_types, bytes)):

if isinstance(address, tuple):

Modification of hosts, only applies when both conditions are failing:

if self._prefixlen == (self._max_prefixlen - 1):
self.hosts = self.iter

The problem has been discovered when using exclude_address, the result is an iterator of IPv4Network objects created using integers. hosts function does not work when one of the remaining IPv4Network is a /31.

Upload to TestPyPI

Would you please be so kind and upload the latest release also to TestPyPi [https://wiki.python.org/moin/TestPyPI]?

I plan to conditionally (i.e. ':python_version == "2.7" or python_version == "3.2"') depend one of my packages on this; since I've never done this before, this needs some serious testing before I upload it to real PyPi, especially with outdated but still common versions of setuptools and pip involved.

If you don't find the time, I guess I can register this package on TestPyPi myself under my own user account, if that's okay with you; but I'd prefer the original author to take all the praise ;-)

IPv6Network misrepresentation

When creating an IPv6Network with python2 / ipaddress-1.0.22, the network representation is screwed up:

[17:21] line:~% virtualenv testp2ipaddress
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/nico/testp2ipaddress/bin/python2
Also creating executable in /home/nico/testp2ipaddress/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
[17:22] line:~% . ./testp2ipaddress/bin/activate
(testp2ipaddress) [17:23] line:~% pip install ipaddress
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting ipaddress
  Using cached https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl
Installing collected packages: ipaddress
Successfully installed ipaddress-1.0.22
(testp2ipaddress) [17:23] line:~% which python
/home/nico/testp2ipaddress/bin/python
(testp2ipaddress) [17:23] line:~% python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.ip_network("2001:db8:61::/64")
IPv6Network(u'3230:3031:3a64:6238:3a36:313a:3a2f:3634/128')
>>>

Expected result as seen in python3:

[17:28] line:~% python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipaddress
>>> ipaddress.ip_network("2001:db8:61::/64")
IPv6Network('2001:db8:61::/64')
>>>

Python 3.2 support

ipaddress was introduced in Python 3.3, so a backport to Python 3.2, which is still widely used, would be nice.

The first problem I ran into when trying to use this module unter Python 3.2 is that it uses the u'string' notation for Unicode strings, which was originally available in 2.x only, and was re-introduced in Python 3.3 (I think).

So to make this work under all Python versions, I suggest to add

from __future__ import unicode_literals

and change all unicode string literals to playin string literals, i.e. u'foo' -> 'foo'. Byte strings can still be specified as b'bar', since that's supported under all relevant Python versions.

If there's interest in porting this module to Python 3.2, I can give it a try myself and provide a pull request eventually.

network in network testing not working

Unable to test if a subnet/network is wholly contained within another -- all comparisons return false:

>>> from ipaddress import *
>>> net1 = ip_network(u"192.0.2.0/24")
>>> net2 = ip_network(u"192.0.2.112/29")
>>> net1 in net2
False
>>> net2 in net1
False
>>>

Passing IPAddress objects to constructor again doesn't work

Python 3.5.2 (default, Sep 26 2016, 21:36:20)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ipaddress import ip_address
>>> ip_address(ip_address('1.1.1.1'))
IPv4Address('1.1.1.1')
Python 2.7.12 (default, Aug  4 2016, 10:57:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ipaddress import ip_address
>>> ip_address(ip_address('1.1.1.1'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/pkg/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: '1.1.1.1' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?

Push tags to github

It would be useful if releases were tagged and pushed as tags. There's a good chance you locally already do so, but haven't pushed them since git doesn't do this by default. You can use git push --tags or if you use annotated tags use followTags to automatically include them on git push.

unicode/bytes behavior is inconvenient

Hi,

I have this piece of software that gives me an str with an ip address on python 2 as well as python 3. passing that into ipaddress, ipaddress barfs on python 2. This is inconvenient for two reasons:

  • I would have to add logic for the python 2 special case
  • Since ipv4 and ipv6 addresses are by definition only made up of a very limited subset of ASCII, there is no point in requiring anyone to use unicode in the first place.

packed fails for IPv6 address

The IPv6 address is 2002::1. So why does packednot include the inner "2" in the IPv6 address?

>>> 
>>> ip = ipaddress.ip_address('2002::1')
>>> ip
IPv6Address('2002::1')
>>> ip.packed
b' \x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
>>> 

README.md not on PyPi

Hey there. Was trying to pip bundle the ipaddress library but it seems the README.md file is not on PyPi and README.txt is linked to it causing an error trying to install it.

Here is a gist: https://gist.github.com/riltsken/6637219

Any chance you could update PyPi with the README.md?

FreeBSD port

Hi Philip,

I'm using 'ipaddress' in my python application 'jadm' which is freebsd jail framework. I want to make a freebsd port for easy distribution. In order to do that i should create freebsd ports for each missing python lib in freebsd ports

'ipaddress' is under PSF license in pypi.python.org which is not clean for third part application can i ask you for you approval to create freebsd port for it. This port still will be with all current license / owner data for 'ipaddress'. Also you will be able to check it in github before submission.

For more information:

port bug: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=193574

jadm app: https://github.com/NikolayDachev/jadm

If i cannot have your approval, i will change my code to not use it.

Regards,
Nikolay

Latest version erroring out on import with PyPy

The error message is:

Traceback (most recent call last):
  File "<builtin>/app_main.py", line 75, in run_toplevel
  File "<builtin>/app_main.py", line 601, in run_it
  File "<string>", line 1, in <module>
  File "/Users/alex_gaynor/.virtualenvs/tempenv-24da2037275be/site-packages/ipaddress.py", line 1358, in <module>
    class IPv4Address(_BaseV4, _BaseAddress):
TypeError: __weakref__ slot disallowed: we already got one

I believe putting __slots__ = () on _TotalOrderingMixin will fix it (but I haven't tested)

Upload a wheel to PyPI

You can achieve this by installing the wheel package and running python setup.py register bdist_wheel upload in your ipaddress directory.

I was originally going to add a setup.cfg in a PR to allow for universal (py2&3) wheel support but realised that would be superfluous given what this project is for!

Add changelog to the project

It would be great if the project would have a changelog so we can easily figure out what changed in each version.

Add notes

Important: Never pass in strs, always unicode!

Shouldn't ipaddress.IPv4Address() return bool instead of rising?

I am aware that it will not be possible to change this due to compatibility issues but would it be possible for the function to have a return value instead of raising an error? I can think of doing this using arguments or perhaps other function. Just a suggestion.

Update Python tracking to support scoped IPv6 addresses

Please update the package to include the recently added support for IPv6 Scoped Literal Addresses added to Python3.9a5 python/cpython@21da76d.

The IPv6 address Wikipedia article has a good description of scoped literal IPv6 addresses — Scoped literal addresses are IPv6 address appended by '%' and a scope or zone id value — typically an interface numeric identifier on Windows and an interface name on Unix-based systems.

Testing using phihag/ipaddress@ d8aef06 version 1.0.23 (latest):

Python 2.7.9 (default, Sep 14 2019, 20:00:08)
[GCC 4.9.2] on linux2

>>> import ipaddress

>>> ipaddress.ip_address(u'::1')
IPv6Address(u'::1')

>>> ipaddress.ip_address(u'::1%1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ipaddress.py", line 168, in ip_address address)
ValueError: u'::1%1' does not appear to be an IPv4 or IPv6 address

Testing using python/cpython@dcd4c4f version 3.9.0a5:

Python 3.9.0a5 (v3.9.0a5:dcd4c4f9c9, Mar 23 2020, 14:59:02)
[Clang 6.0 (clang-600.0.57)] on darwin

>>> import ipaddress

>>> ipaddress.ip_address(u'::1')
IPv6Address('::1')

>>> ipaddress.ip_address(u'::1%1')
IPv6Address('::1%1')

(For background, this is normal behaviour for Python's socket.socket.accept(), which is a transliteration of the underlying system call. On Unix-based systems (such as Linux), sockaddr_storage (sockaddr_in6 from ip6(7)) includes sin6_scope_id (ifr_ifindex from netdevice(7)), and is returned as the interface name (represented as %ifr_ifname) when being returned as a string by the Python2.7 sockets library (python/cpython@7a41638 2.7.18rc1/Modules/socketmodule.c#L1366).

Update to version from Python 3.8

Hello

I'd really like to see #55 fixed and released via #56 but it might be also possible to update this module to the version currently available in Python 3.8 (for start).

I'd like to be proactive here but I need to know what are your plans with backward compatibility. Are you okay to break backward compatibility to stay close to upstream or not? For example, this commit changes how ipaddress handles leading zeros in IPv4 addresses.

I think I've found all commits we have to backport to update to the version we have in Python 3.8 now. The last commit from upstream I see implemented here is python/cpython@91dc64b#diff-f67a8b5be6bdc48383d2ab907325a4c75ef998fa7cd0bba382ec2efa7e4f00b5
The list of commits: https://github.com/python/cpython/commits/3.8/Lib/ipaddress.py
For example, the first commit in the list to backport (python/cpython@58a1096) won't be backported because it uses str.isascii() available only in 3.7 and newer.

Any help appreciated!

buildout legacy distribution

I am getting an error while getting distribution for 'ipaddress==1.0.22' using buildout which is not using wheel, but legacy source. The distribution for version 1.0.21 indeed can be found using buildout. Is this a case of a caching issue w/ pypi? I noticed the setup.py in the master branch still points to 1.0.21

Carlos

IPv4Network.address_exclude does not work with /32

This example works, attempting to exclude a /31:

>>> from __future__ import unicode_literals
>>> from ipaddress import ip_network
>>> n = ip_network('10.0.0.0/24')
>>> n2 = ip_network('10.0.0.2/31')
>>> ns = list(n.address_exclude(n2))
>>> ns
[IPv4Network(u'10.0.0.128/25'), IPv4Network(u'10.0.0.64/26'), IPv4Network(u'10.0.0.32/27'), IPv4Network(u'10.0.0.16/28'), IPv4Network(u'10.0.0.8/29'), IPv4Network(u'10.0.0.4/30'), IPv4Network(u'10.0.0.0/31')]

However, if I change n2 to be a /32, I get...

>>> n2 = ip_network('10.0.0.2/32')
>>> ns = list(n.address_exclude(n2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jwilhelm/.virtualenvs/thor_api/lib/python2.7/site-packages/ipaddress.py", line 921, in address_exclude
    s1, s2 = s2.subnets()
ValueError: need more than 1 value to unpack
>>>

It appears that this same bug exists upstream as well in Python 3.5.1.

ip_network not working correctly

When checking an IP/network definition by using "ipaddress.ip_network", apparenty the function doesn't recognize IPs that don't have a standard 3 digit format. Examples:

Successful validation:
107.167.160.0/19
107.178.192.0/18
108.170.192.0/20
108.170.208.0/21

Error:
104.196.0.0/14
208.68.108.0/23
35.184.0.0/14
35.188.0.0/15

CWE-20 Input Verification Vulnerabilities

In Python before 3,9,5, the ipaddress library mishandles leading zero characters in the octets of an IP address string. This (in some situations) allows attackers to bypass access control that is based on IP addresses.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.