tehmaze / ipcalc Goto Github PK
View Code? Open in Web Editor NEWPython IP Calculator
Home Page: http://pypi.python.org/pypi/ipcalc
License: BSD 2-Clause "Simplified" License
Python IP Calculator
Home Page: http://pypi.python.org/pypi/ipcalc
License: BSD 2-Clause "Simplified" License
It needs a README.rst file inside it, as referred to by the setup.py.
Getting distribution for 'ipcalc'.
error: README.rst: No such file or directory
An error occured when trying to install ipcalc 0.4.1. Look above this message for any errors that were output by easy_install.
Currently there are failing two tests: Especially the second test is important. Is it a typo?
File "ipcalc.py", line 361, in
ipcalc.IP.clone
Failed example:ip.clone()Expected:
<ipcalc.IP object at 0xb7d4d18c>Got:
<ipcalc.IP object at 0x980504c>
File "ipcalc.py", line 534, in ipcalc.Network.iter
Failed example:for ip in Network('192.168.114.0.40'): print str(ip)Exception raised:
Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1254, in __run compileflags, 1) in test.globs File "<doctest ipcalc.Network.__iter__[0]>", line 1, in <module> for ip in Network('192.168.114.0.40'): File "ipcalc.py", line 161, in __init__ self.ip = self._dqtoi(ip) File "ipcalc.py", line 301, in _dqtoi raise ValueError, "%r: IPv4 address invalid: more than 4 bytes" % dq ValueError: '192.168.114.0.40': IPv4 address invalid: more than 4 bytes
2 items had failures:
1 of 2 inipcalc.IP.clone
1 of 1 inipcalc.Network.__iter__
_Test Failed_ 2 failures.
When installing ipcalc 1.99.0 via pip, it did not install six. Importing ipcalc without having six installed will fail.
Hi
I am trying to install ipcalc with pip on python version 3.4.1 and get this message:
Installing collected packages: ipcalc
Running setup.py install for ipcalc
File "C:\Python34\Lib\site-packages\ipcalc.py", line 237
if ip > 0xffffffffffffffffffffffffffffffffL:
^
SyntaxError: invalid syntax
Is it possible, that you can fix that?
Kind regard
Adrian
Do you plan to support python3? I have problems using this library in archlinux with python3.
Cygwin ~ $ ic 1.1.1.1/31
first host: 1.1.1.1/31
last host.: 1.1.1.2
The bug report for this initially:#17 The fix is not correct as seen above.
There is a cast to IP when comparing (e.g. __eg__()
, __lt__()
, etc...) in the Network class. I suspect this is a copy-n-paste error?
Anyhow, changing from IP(other).size()
to Network(other).size()
solved my problem when comparing instances of network classes.
BR
Markus
Cygwin ~ $ ic 1.1.1.1/31
first host: 1.1.1.1/31
last host.: 1.1.1.2
The bug report for this initially:#17 The fix is not correct as seen above.
This may be a fix for the above bug:
def host_first(self):
if (self.version() == 4 and self.mask > 30) or \
(self.version() == 6 and self.mask > 126):
-return self
+return self.network()
...
def host_last(self):
"""Last available host in this subnet."""
if (self.version() == 4 and self.mask == 32) or \
...
elif (self.version() == 4 and self.mask == 31) or \
(self.version() == 6 and self.mask == 127):
-return IP(int(self) + 1, version=self.version())
+return IP(int(self.network()) + 1, version=self.version())
If I try to use a negative indice it subtracts from the IP subnet instead of returning an IP from the end of netblock.
For example:
foo1 = ipcalc.Network('192.243.234.0/29')
print foo1[-1]
192.243.233.255
I would expect it to return 192.243.234.7 which is the last IP in the 192.243.234.0/29 netblock.
This may be working as expected but it would be very handy to be able to indices IPs from the end of the subnet moving toward the beginning.
This line:
netmask = 0x100000000 - 2**(32-self.mask)
should not use exponentiation, it should use bit-shifts. Exponents coerce the expression to float and the code after fails when it tries to use "&".
Probably should return a ValueError, or some such when init is passed None?
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
> > > from ipcalc import *
> > > ip = IP(u'10.157.199.130/25')
> > > ip in [ None ]
> > > Traceback (most recent call last):
> > > File "<stdin>", line 1, in <module>
> > > File "/Library/Python/2.7/site-packages/ipcalc.py", line 351, in **eq**
> > > return long(self) == long(IP(other))
> > > File "/Library/Python/2.7/site-packages/ipcalc.py", line 156, in **init**
> > > if '/' in ip:
> > > TypeError: argument of type 'NoneType' is not iterable```
Hi
I've just tested your python module in short sample script but i have an issue :
AttributeError: partially initialized module 'ipcalc' has no attribute 'Network' (most likely due to a circular import)
My script is just
import ipcalc
for x in ipcalc.Network('10.20x.1xx.0/24'):
print(str(x))
https://github.com/tehmaze/ipcalc/blob/master/src/ipcalc.py#L190
The code is quite complicated, but if understand the API, it only converts self.ip (a long) to its binary representation as a string.
so maybe : this could be sufficient?
def bin(self): return '{0:b}'.format(self.ip)
When parsing a mixed (or 4-in-6) address the '6' part is stripped and only the IPv4 part is returned in the resulting integer.
Eg (using ipython):
In [1]: from ipcalc import IP
In [2]: x = IP("fd::0.0.0.1")
In [3]: x.ip
Out[3]: 1
Why does ipcalc.IP() excepts xxx.xxx/xx?
There is no source code license specified for this project.
I would like to request that this library generates Cisco style wildcard masks, possibly with Network.wildcard().
For example, 10.1.1.1/30 = 10.1.1.1/255.255.255.252 with a wildcard mask of 0.0.0.3.
Will you include the capacity to reverse ipv6s for ptrs?
for example the nestat -nr
command on OSX outputs truncated network names where for example 10.255/20
is considered to be equivalent of 10.255.0.0/20
. For ipcalc these are not treated equally:
>>> n = ipcalc.Network("10.255/20")
>>> n.host_first(), n.host_last()
(IP('10.0.0.1'), IP('10.0.15.254'))
>>> n = ipcalc.Network("10.255.0.0/20")
>>> n.host_first(), n.host_last()
(IP('10.255.0.1'), IP('10.255.15.254'))
(python-env)date:~ noa$ pip list |grep ipcalc
ipcalc (1.1.3)
I think it's an untested corner case -- basically call to network with 0.0.0.0 as ip and mask causes the function to hang. The error occurs when the mask is '0.0.0.0'
import ipcalc foo = ipcalc.Network('fd00:dead:ca7:cafe::1/64') foo.__iter__().next() Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/site-packages/ipcalc.py", line 544, in __iter__ for ip in (IP(long(self)+x) for x in xrange(0, self.size())): OverflowError: Python int too large to convert to C long
There is a typo in line 169:
# Netmask is numeric CIDR subnet
elif type(self.mask) in [int, long] or self.mask.isdigit():
self.mask = int(mask) # typo!!!
The line should read:
self.mask = int(self.mask)
RFC 3021 specifies an exception to this rule when dealing with 31-bit subnet masks (i.e. 1-bit host identifiers). In such networks, usually point-to-point links, only two hosts (the end points) may be connected and a specification of network and broadcast addresses is not necessary.
suggested addition to host first:
def host_first(self):
if (self.version() == 4 and self.mask == 31) or (self.version() == 6 and self.mask == 127):
return self.network()
suggested addition to host last:
def host_last(self):
if (self.version() == 4 and self.mask == 31) or (self.version() == 6 and self.mask == 127):
return self.broadcast()
I think you are doing strange calculations. (You aren't doing things with binary (and,or,xor) operations?)
The three following examples are plain wrong.
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import ipcalc
ipcalc.version
'0.5'
from ipcalc import Network
n = Network("129.0.0.0/2")
"128.0.0.0" in n
False
n.in_network("128.0.0.1")
False
"192.0.0.0" in n
True
How to reproduce:
for ip in Network('127.0.0.1/24'):
print ip
Last line prints 127.0.1.0
which it shouldn't.
Solution: * EDIT:* does not work!
def __iter__(self):
# ...
for ip in (IP(long(self)+x) for x in xrange(0, self.size())):
yield ip
Change to:
for ip in (IP(long(self)+x) for x in xrange(0, self.size()-1)):
Using software ipcal:
willian_pires@wpsXPS:~/Programas/python3/IPS_FROM_REGION$: ipcalc 31.135.244.0/21
Address: 31.135.244.0 00011111.10000111.11110 100.00000000
Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
Wildcard: 0.0.7.255 00000000.00000000.00000 111.11111111
=>
Network: 31.135.240.0/21 00011111.10000111.11110 000.00000000
HostMin: 31.135.240.1 00011111.10000111.11110 000.00000001
HostMax: 31.135.247.254 00011111.10000111.11110 111.11111110
Broadcast: 31.135.247.255 00011111.10000111.11110 111.11111111
Hosts/Net: 2046 Class A
This is network address: 31.135.240.0/21 and this is BroadCast Address: 31.135.247.255
When i run this code:
import ipcalc
address = '31.135.244.0/21'
c = ipcalc.Network(address)
print(c.broadcast,c.network)
it produce:
<bound method Network.broadcast of Network('31.135.244.0/21')>
<bound method Network.network of Network('31.135.244.0/21')>
Look at Network address is not 31.135.240.0/21
Hello,
Using python2.6 (centos6) I've found the following:
$ python
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ipcalc
>>> subnet = ipcalc.Network('128.121.0.0/16')
>>> rentry = ipcalc.Network('128.121.159.48/28')
>>> subnet in rentry
True
>>> rentry in subnet
True
And it's ipcalc version 1.1.3
Are you exactly sure that the above expression should return true in both direction? Personally I think a /16 should have difficulties in being a subset of a /28. It's very hard to create a /28 superset which covers a /16.
This was brought up in issue #4, but I was still getting strange results from in_network(). Below works well enough for my purposes, but I'm really rather new to Python.
def in_network(self, other):
'''
Check if the given IP address is within this network.
'''
other = Network(other)
return(long(self.netmask())&long(self) == long(self.netmask())&long(other))
Edit: Above code edited to actually mask address and compare network. It returns true for network and broadcast addresses which I'm not sure is desired.
Consider adding additional special _range identifications for IPv4 subnets commonly used in the telecom industry:
A non-exhaustive list is here:
http://en.wikipedia.org/wiki/Reserved_IP_addresses
ipcalc doesn't correctly calculate usable IP space if you supply a usable IP address into the Network IP variable.
For example:
for x in ipcalc.Network("192.168.1.5",mask=24):
print x
returns
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
192.168.1.21
192.168.1.22
192.168.1.23
192.168.1.24
192.168.1.25
192.168.1.26
192.168.1.27
192.168.1.28
192.168.1.29
192.168.1.30
192.168.1.31
192.168.1.32
192.168.1.33
192.168.1.34
192.168.1.35
192.168.1.36
192.168.1.37
192.168.1.38
192.168.1.39
192.168.1.40
192.168.1.41
192.168.1.42
192.168.1.43
192.168.1.44
192.168.1.45
192.168.1.46
192.168.1.47
192.168.1.48
192.168.1.49
192.168.1.50
192.168.1.51
192.168.1.52
192.168.1.53
192.168.1.54
192.168.1.55
192.168.1.56
192.168.1.57
192.168.1.58
192.168.1.59
192.168.1.60
192.168.1.61
192.168.1.62
192.168.1.63
192.168.1.64
192.168.1.65
192.168.1.66
192.168.1.67
192.168.1.68
192.168.1.69
192.168.1.70
192.168.1.71
192.168.1.72
192.168.1.73
192.168.1.74
192.168.1.75
192.168.1.76
192.168.1.77
192.168.1.78
192.168.1.79
192.168.1.80
192.168.1.81
192.168.1.82
192.168.1.83
192.168.1.84
192.168.1.85
192.168.1.86
192.168.1.87
192.168.1.88
192.168.1.89
192.168.1.90
192.168.1.91
192.168.1.92
192.168.1.93
192.168.1.94
192.168.1.95
192.168.1.96
192.168.1.97
192.168.1.98
192.168.1.99
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.105
192.168.1.106
192.168.1.107
192.168.1.108
192.168.1.109
192.168.1.110
192.168.1.111
192.168.1.112
192.168.1.113
192.168.1.114
192.168.1.115
192.168.1.116
192.168.1.117
192.168.1.118
192.168.1.119
192.168.1.120
192.168.1.121
192.168.1.122
192.168.1.123
192.168.1.124
192.168.1.125
192.168.1.126
192.168.1.127
192.168.1.128
192.168.1.129
192.168.1.130
192.168.1.131
192.168.1.132
192.168.1.133
192.168.1.134
192.168.1.135
192.168.1.136
192.168.1.137
192.168.1.138
192.168.1.139
192.168.1.140
192.168.1.141
192.168.1.142
192.168.1.143
192.168.1.144
192.168.1.145
192.168.1.146
192.168.1.147
192.168.1.148
192.168.1.149
192.168.1.150
192.168.1.151
192.168.1.152
192.168.1.153
192.168.1.154
192.168.1.155
192.168.1.156
192.168.1.157
192.168.1.158
192.168.1.159
192.168.1.160
192.168.1.161
192.168.1.162
192.168.1.163
192.168.1.164
192.168.1.165
192.168.1.166
192.168.1.167
192.168.1.168
192.168.1.169
192.168.1.170
192.168.1.171
192.168.1.172
192.168.1.173
192.168.1.174
192.168.1.175
192.168.1.176
192.168.1.177
192.168.1.178
192.168.1.179
192.168.1.180
192.168.1.181
192.168.1.182
192.168.1.183
192.168.1.184
192.168.1.185
192.168.1.186
192.168.1.187
192.168.1.188
192.168.1.189
192.168.1.190
192.168.1.191
192.168.1.192
192.168.1.193
192.168.1.194
192.168.1.195
192.168.1.196
192.168.1.197
192.168.1.198
192.168.1.199
192.168.1.200
192.168.1.201
192.168.1.202
192.168.1.203
192.168.1.204
192.168.1.205
192.168.1.206
192.168.1.207
192.168.1.208
192.168.1.209
192.168.1.210
192.168.1.211
192.168.1.212
192.168.1.213
192.168.1.214
192.168.1.215
192.168.1.216
192.168.1.217
192.168.1.218
192.168.1.219
192.168.1.220
192.168.1.221
192.168.1.222
192.168.1.223
192.168.1.224
192.168.1.225
192.168.1.226
192.168.1.227
192.168.1.228
192.168.1.229
192.168.1.230
192.168.1.231
192.168.1.232
192.168.1.233
192.168.1.234
192.168.1.235
192.168.1.236
192.168.1.237
192.168.1.238
192.168.1.239
192.168.1.240
192.168.1.241
192.168.1.242
192.168.1.243
192.168.1.244
192.168.1.245
192.168.1.246
192.168.1.247
192.168.1.248
192.168.1.249
192.168.1.250
192.168.1.251
192.168.1.252
192.168.1.253
192.168.1.254
192.168.1.255
192.168.2.0
192.168.2.1
192.168.2.2
192.168.2.3
192.168.2.4
while technically correct with the given inputs, the more common use-case would be for someone to attempt to resolve a usable IP address space of an IP + Netmask. I didn't see a way to retrieve the Network Address from the IP object which would solve the problem here.
I would hope that we can do something like this:
for x in ipcalc.IP("192.168.1.1", mask=24).network():
print x
File "/Users/nagakasu/deploy/uRevenue/env/lib/python2.7/site-packages/ipcalc.py", line 152, in __init__ self.ip = self._dqtoi(ip) File "/Users/nagakasu/deploy/uRevenue/env/lib/python2.7/site-packages/ipcalc.py", line 278, in _dqtoi if not 0 <= int(h, 16) <= 0xffff: ValueError: invalid literal for int() with base 16: '1%lo0'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.